Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Ace_Oxide · Sep 20, 2017 at 07:03 PM · animationrotationtranslation

How to make object face the direction it's moving?

Ok, so I have a little character dude and I've set him up to move with WSAD keys. It's a top-down RPG, for the visual.

 if(Input.GetKey(KeyCode.W)){
         transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime, Space.World);
         transform.LookAt (Vector3.forward);
     }
     if(Input.GetKey(KeyCode.S)){
         transform.Translate (-Vector3.forward * moveSpeed * Time.deltaTime, Space.World);
         transform.LookAt (-Vector3.forward);
     }
     if(Input.GetKey(KeyCode.A)){
         transform.Translate (Vector3.left * moveSpeed * Time.deltaTime, Space.World);
         transform.LookAt (Vector3.left);
     }
     if(Input.GetKey(KeyCode.D)){
         transform.Translate (Vector3.right * moveSpeed * Time.deltaTime, Space.World);
         transform.LookAt (Vector3.right);
     }

So obviously I'm using transform.LookAt wrong, because I realize that just points to the point in world space which just kind of makes him twitch and look to the one area. I was previously using a click-move system but that was really getting on my nerves so I changed it up to this, but now I'm not quite sure how to go about making the character face the correct direction he's moving. I mean there are only 8 directions it's possible for him to move, so I could hard code all eight possibilities with exact angles... But that's a bit constrictive and I'm trying to be more thoughtful about my code so I figure instead of having 8 separate checks going on every Update(), I could just point him in his current direction once?

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

2 Replies

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

Answer by Arocide · Sep 21, 2017 at 05:52 PM

If you were to make your little character dude look at the new position before translating them they should look to where they will move so for example:

     private void Update()
     {
         float moveVertical = Input.GetAxis("Vertical");
         float moveHorizontal = Input.GetAxis("Horizontal");
 
         Vector3 newPosition = new Vector3(moveHorizontal, 0.0f, moveVertical);
         transform.LookAt(newPosition + transform.position);
         transform.Translate(newPosition * moveSpeed * Time.deltaTime, Space.World);
     }

Would achieve the desired effect, by using Axis instead of Keys directly you can also shorten the code to what is above as you can use the values that the Axis give directly (They are Analogue and such use a float). While this will just drag and drop since by default WASD are already mapped to the virtual axis created by unity you can also change them in the input manager (If for example you'd like for the game to support a controller natively) or through scripts making them very handy. I'd suggest using the virtual axis when possible and they make sense to.

Comment
Add comment · Show 3 · 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 Ace_Oxide · Sep 21, 2017 at 09:29 PM 0
Share

Absolutely awesome answer! Exceeded what I was looking for, thank you!

avatar image ballpunn · Mar 14, 2019 at 04:24 AM 0
Share

still, work in 3/14/2019 I would like to thank you so much you save my day

avatar image robonobo · Nov 22, 2020 at 06:37 AM 0
Share

A correct and concise answer hidden deep inside a web search of wrongness!

avatar image
0

Answer by Jodo_kin · Oct 28, 2020 at 11:20 PM

@Arocide This movement script is buttery as hell thank you!

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

238 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 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

Sprite rotation clip & ending position 0 Answers

Smooth rotation on animation 1 Answer

RotateAround wont rotate to starting position 0 Answers

How can I check what the closest object to a player is that he's rotating towards without using a raycast? 0 Answers

Visible Rotation from controller 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