• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by LukeNewsome1 · Apr 07, 2020 at 01:17 AM · rotationmovement2d gamerotatetowardsturn

Turn character the way I am moving

Hi I’ve been trying to be able to get my character to turn the way it’s moving all day and couldn’t do it could someone please just give me something that I could copy and paste into my script thank you

EDIT: I have problems like this a lot and everything is the exact way the person had it even when I copy and paste could something be wrong with my unity or something because it can’t be code when I copy and paste when it’s not working it’s very discouraging and happens a lot

EDIT: I noticed that the rotation in the inspector changes to 90.00001 or -90.00001 depending on the way I move the arrows on what ever axis I don't know if that's normal but yeah and one time the y position not rotation moved to 900000 when I try to move but that stopped happening. Sorry for editing so much.

EDIT: Here is my code right now without the changes also im a beginner so how do I do that I just want the character to turn so if I press left arrow he moves left and I want to make it so he rotates left too I saw a bunch of things about quarteron angle and stuff and it confused me and didn't work with my code probably because I had to delete something. using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { [SerializeField] float moveSpeed = 10f; [SerializeField] float padding = 1f; float xMin; float xMax; float yMin; float yMax;

// Start is called before the first frame update void Start() { SetMoveBoundaries(); }

private void SetMoveBoundaries() { Camera gameCamera = Camera.main; xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding; xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding; yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding; yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;

}

// Update is called once per frame void Update() { Move();

}

private void Move() { var deltaX = Input.GetAxis("Horizontal") Time.deltaTime moveSpeed; var newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax); var deltaY = Input.GetAxis("Vertical") Time.deltaTime moveSpeed; var newYPos = Mathf.Clamp(transform.position.y + deltaY, yMin, yMax); transform.position = new Vector2(newXPos, newYPos);

} }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BBIT-SOLUTIONS · Apr 07, 2020 at 02:49 AM 0
Share

Would be useful to see some code, how you move the character at the moment in your game. Then we can help you with the turning problem for your specific situation, because there are different approaches, how you could achieve this.

avatar image LukeNewsome1 · Apr 07, 2020 at 04:16 AM 0
Share

Here is my code right now without the changes also im a beginner so how do I do that I just want the character to turn so if I press left arrow he moves left and I want to make it so he rotates left too I saw a bunch of things about quarteron angle and stuff and it confused me and didn't work with my code probably because I had to delete something. using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : $$anonymous$$onoBehaviour { [SerializeField] float moveSpeed = 10f; [SerializeField] float padding = 1f; float x$$anonymous$$in; float x$$anonymous$$ax; float y$$anonymous$$in; float y$$anonymous$$ax;

// Start is called before the first frame update void Start() { Set$$anonymous$$oveBoundaries(); }

private void Set$$anonymous$$oveBoundaries() { Camera gameCamera = Camera.main; x$$anonymous$$in = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding; x$$anonymous$$ax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding; y$$anonymous$$in = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding; y$$anonymous$$ax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;

}

// Update is called once per frame void Update() { $$anonymous$$ove();

}

private void $$anonymous$$ove() { var deltaX = Input.GetAxis("Horizontal") Time.deltaTime moveSpeed; var newXPos = $$anonymous$$athf.Clamp(transform.position.x + deltaX, x$$anonymous$$in, x$$anonymous$$ax); var deltaY = Input.GetAxis("Vertical") Time.deltaTime moveSpeed; var newYPos = $$anonymous$$athf.Clamp(transform.position.y + deltaY, y$$anonymous$$in, y$$anonymous$$ax); transform.position = new Vector2(newXPos, newYPos);

} }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vibertex · Apr 07, 2020 at 04:03 AM

@LukeNewsome1 Try setting the Euler angles to the mouse pointer or whatever way you want it to change direction and *IMPORTANT*make sure that you select local at the top left when you select the character

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LukeNewsome1 · Apr 07, 2020 at 04:11 AM 0
Share

Here is my code right now without the changes also im a beginner so how do I do that I just want the character to turn so if I press left arrow he moves left and I want to make it so he rotates left too I saw a bunch of things about quarteron angle and stuff and it confused me and didn't work with my code probably because I had to delete something.

using System; using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player : $$anonymous$$onoBehaviour {

 [SerializeField] float moveSpeed = 10f;
 [SerializeField] float padding = 1f;
 float x$$anonymous$$in;
 float x$$anonymous$$ax;
 float y$$anonymous$$in;
 float y$$anonymous$$ax;

 // Start is called before the first frame update
 void Start()
 {
     Set$$anonymous$$oveBoundaries();
 }







 private void Set$$anonymous$$oveBoundaries()
 {
     Camera gameCamera = Camera.main;
     x$$anonymous$$in = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding;
     x$$anonymous$$ax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding;
     y$$anonymous$$in = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding;
     y$$anonymous$$ax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding;
         
 }
    

 // Update is called once per frame
 void Update()
 {
     $$anonymous$$ove();

     

 }



 private void $$anonymous$$ove()
 {
     var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
     var newXPos = $$anonymous$$athf.Clamp(transform.position.x + deltaX, x$$anonymous$$in, x$$anonymous$$ax);
     var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
     var newYPos = $$anonymous$$athf.Clamp(transform.position.y + deltaY, y$$anonymous$$in, y$$anonymous$$ax);
     transform.position = new Vector2(newXPos, newYPos);

     
 }

}

avatar image
0

Answer by BBIT-SOLUTIONS · Apr 07, 2020 at 03:26 PM

Thanks for your code, that was helpful. I guess all you need is this line as first line of your Move()-function

 Vector3 currPos = transform.position;

And as last line in the same function:

 transform.forward = transform.position - currPos;

Or if it is maybe rotated, then try transform.up or transform.right instead of transform.forward

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LukeNewsome1 · Apr 07, 2020 at 04:49 PM 0
Share

Hi thanks for your reply I did what you said and my character disappears and a warning appeared saying viewing vector is zero Its not appearing anymore tho so I don't know if that was a problem

avatar image LukeNewsome1 · Apr 07, 2020 at 04:51 PM 0
Share

never $$anonymous$$d it still says look rotation viewing vector is zero and it only disappears when I move my character then it reappears

avatar image BBIT-SOLUTIONS LukeNewsome1 · Apr 07, 2020 at 06:49 PM 0
Share

Ok strange. I tested your code with my additional lines and everything worked actually.

Could you maybe make a screenshot of your error message in the console and from your scene, hierarchy and inspector? $$anonymous$$aybe something is wrong there...

avatar image BBIT-SOLUTIONS BBIT-SOLUTIONS · Apr 07, 2020 at 06:56 PM 0
Share

Oh no, sorry. Have to correct me. You are right, if they are both the same value, forward would show to Zero vector, what is not defined, so we have to check, if it has really changed during this frame. (Just not sure, why i didn't get the error as i tried before) Try the following:

 private void $$anonymous$$ove()
 {
     Vector3 currPos = transform.position;
 
      var deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
      var newXPos = $$anonymous$$athf.Clamp(transform.position.x + deltaX, x$$anonymous$$in, x$$anonymous$$ax);
      var deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
      var newYPos = $$anonymous$$athf.Clamp(transform.position.y + deltaY, y$$anonymous$$in, y$$anonymous$$ax);
      transform.position = new Vector2(newXPos, newYPos);
 
     if(currPos != transform.position){
         transform.forward = transform.position - currPos;
     }
      
  }
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Zig-zag movement and rotation 1 Answer

how to rotate a gameobject in a given direction and stop when it's done 1 Answer

Sprite will not roate when arrow keys are pressed 2 Answers

Change the z rotation of my player?,how to adjust the z rotation of my player? 1 Answer

Combining 2 camera systems 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges