Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Andrewlo3 · Sep 20, 2014 at 07:36 AM · 2djavascriptactionscript

How to rotate character to movement direction

I am making a top down movement script and I need my character to rotate in the direction I am running. I can't use transform.rotate because I need the character to always rotate to the left when i press "A" and so on. Here is my code so far:

 #pragma strict
 
 var movementSpeed = 0.5;
 
 function Start () {
 
 }
 
 function Update () {
 
     //WASD MOVEMENT
     if(Input.GetKey(KeyCode.A)) {
         transform.Translate(-Vector2.right * movementSpeed * Time.deltaTime, Space.World);
     }
 
     if(Input.GetKey(KeyCode.D)) {
         transform.Translate(Vector2.right * movementSpeed * Time.deltaTime, Space.World);
     }
 
     if(Input.GetKey(KeyCode.W)) {
         transform.Translate(Vector2.up * movementSpeed * Time.deltaTime, Space.World);
     }
 
     if(Input.GetKey(KeyCode.S)) {
         transform.Translate(-Vector2.up * movementSpeed * Time.deltaTime, Space.World);
     }    
 
     //ARROW KEY TURNING
     if(Input.GetKeyDown(KeyCode.A)) {
         //transform.Rotate(Vector3.back, Space.World);
         transform.rotation.x += 1;
     }
 
     if(Input.GetKeyDown(KeyCode.D)) {
         //transform.Rotate(Vector3.back, Space.World);
     }
 
     if(Input.GetKeyDown(KeyCode.W)) {
         //transform.Rotate(Vector3.back, Space.World);
     }
 
     if(Input.GetKeyDown(KeyCode.S)) {
         //transform.Rotate(Vector3.back, Space.World);
     }
 }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Sep 20, 2014 at 07:49 AM

One solution is to save the position of the previous frame and use the change between the frames as a direction. Below is an untested rewrite to your rotation code. It assumes 1) 2D (since you are moving on the XY plane, and 2) that your sprite or quad has the front side on the right when the rotation is (0,0,0).

 #pragma strict
  
 var movementSpeed = 0.5;
 
 private var prevPos : Vector3;
  
 function Start () {
     prevPos = transform.position;
 }
  
 function Update () {
  
     //WASD MOVEMENT
     if(Input.GetKey(KeyCode.A)) {
         transform.Translate(-Vector2.right * movementSpeed * Time.deltaTime, Space.World);
     }
  
     if(Input.GetKey(KeyCode.D)) {
         transform.Translate(Vector2.right * movementSpeed * Time.deltaTime, Space.World);
     }
  
     if(Input.GetKey(KeyCode.W)) {
         transform.Translate(Vector2.up * movementSpeed * Time.deltaTime, Space.World);
     }
  
     if(Input.GetKey(KeyCode.S)) {
         transform.Translate(-Vector2.up * movementSpeed * Time.deltaTime, Space.World);
     }   
 
     if (prevPos != transform.position) {
         dir = transform.position - prevPos;
         angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
         prevPos = transform.position;
     }
 }

Note that this code causes an immediate rotation change. You could add Slerp()ing code, but then the object would be out of alignment for some period of the move in a new direction.

Comment
Add comment · Show 2 · 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 Andrewlo3 · Sep 20, 2014 at 04:55 PM 0
Share

@robertbu - thanks so much! Out of curiosity, how would I change the code if my sprite were facing down when the rotation is (0, 0, 0)?

avatar image robertbu · Sep 20, 2014 at 05:45 PM 0
Share

The best solution is to rotate the texture in your favorite image editor, but I believe you can subtract 90 from the angle passed to AngleAxis:

     transform.rotation = Quaternion.AngleAxis(angle - 90.0, Vector3.forward);
  
avatar image
0

Answer by drudiverse · Sep 20, 2014 at 07:47 AM

Hi there, First of all, you should use EulerAngles to rotate your char NESW north east south west, or never eat shredded wheat whichever you prefer....

 A = transorm.eulerAngles = (0,0,0)
 or player.transform.rotation = quaternion.eulerAngles (0,90,0)
 A = transorm.eulerAngles = (0,90,0)
 A = transorm.eulerAngles = (0,180,0)
 A = transorm.eulerAngles = (0,360,0)

so you have the quaternion rotations that you want in easy format. then you use Slerp to rotate between current angle and new one, and try different lerp speeds to find the right one.

you can also use transform.lookAt (0,99999,0) if you want, to point your player to different angles on screen.

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 robertbu · Sep 20, 2014 at 07:52 AM 0
Share

@drudiverse - a solution along your lines was my first thought, but the way the code is written now, he can press two keys at the same time resulting in diagonal movement.

avatar image
0

Answer by Sanerhp228yt · Apr 18, 2019 at 04:58 PM

Unknown identifier "dir"

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change gravity on button 1 Answer

2D: Rotating to face mouse position with two fixed axes 1 Answer

Basic 2D cannon rotation code not working 1 Answer

Uni2D Play() - Misunderstanding 3 Answers

Why does my enemies' movement change whenever my player moves toward and away from the enemies' current position? 1 Answer


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