Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CreativeNess · Oct 16, 2017 at 12:57 AM · movementcharactercontrollerdirection

How to make character face towards movement direction?

For the past couple of days I have been looking through all the unity answers and questions to find a solution to this problem. Now, I have found a sort of solution but it doesn't work perfectly.
Not only does my character not turn go in the direction that it is facing but when I push my "turning buttons" (this causes the player to turn clockwise or counterclockwise) it causes the players to start rolling in a random direction. Lastly, for some odd reason that I can't even fathom why, my up/forward button goes completely to the right. The left goes the direction the forward/up button should go. I have tried starting my player at a different rotation or just moving the camera but it continues to go right.

Any help at this point would be deeply appreciated. Below is my script for my player controller.

{

public GameObject PlayerOne;

 public float Movement;
 public Rigidbody rb = null;
 public float speed = 5.0f;
 public float movementSpeed = 5.0f;
 public float clockwise = 10.0f;
 public float counterClockwise = -10.0f;

 // Use this for initialization
 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }

 // Update is called once per frame
 void Update()
 {
     
     if (Input.GetKey (KeyCode.W)) {
         transform.position += transform.forward * Time.deltaTime * movementSpeed;
     } else if (Input.GetKey (KeyCode.S)) {
         transform.position += Vector3.back * Time.deltaTime * movementSpeed;
     } else if (Input.GetKey (KeyCode.A)) {
         transform.position += Vector3.left * Time.deltaTime * movementSpeed;
     } else if (Input.GetKey (KeyCode.D)) {
         transform.position += Vector3.right * Time.deltaTime * movementSpeed;
     }
     if (Input.GetKey (KeyCode.E)) {
         transform.Rotate (0, Time.deltaTime * clockwise, 0);
     } else if (Input.GetKey (KeyCode.Q)) {
         transform.Rotate (0, Time.deltaTime * counterClockwise, 0);
     }
 }
         

}

Comment
Add comment · Show 3
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 toddisarockstar · Oct 16, 2017 at 04:12 AM 0
Share

hey bro, i think you might be a bit mixed up looking at your code. i assume you have a 3D first person point of view?

when you say Vector3.right, that's world direction! it's just like saying "move east reguardless of which way you are facing".

if you want your character to move to move to his right then you say transform.right.

if you put a negative in front of any direction or speed you get the opposite.... so left is simply -transform.right and backwords is simply -transform.forward.

I'm guessing this is what you want for "a" and "d" and "s" keys

(if you are not first person and have a fixed rotation camera, then you would want the global Vector3 directions. )

avatar image CreativeNess toddisarockstar · Oct 18, 2017 at 02:24 AM 0
Share

I tried what you had suggested and changed the Vector3 to transform but now I have compiler errors saying that there is no definition for transform.back or transform.left.

(By the way, if that was literally the only problem with my code and the next answer is super easy and convenient like this answer I might scream because why didn't I think of that.)

avatar image toddisarockstar CreativeNess · Oct 19, 2017 at 10:18 PM 1
Share

i all ready mentioned a soulution to your errors your talking about in my previous instructions. there is no transform.back or transform.left. you use a negative sign with right and forward as i mentioned above!

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CptKen · Oct 18, 2017 at 05:00 AM

Hi,

Instead of directly modifying the position of the transform, try using Transform.Translate with it set to self space.

https://docs.unity3d.com/ScriptReference/Transform.Translate.html

In this case you can use Vector3.forward, Vector3.right etc. instead of transform.forward because the translation will occur in local space and not world space.

If you want the character to always be rotated in the direction of travel you need to use the following

https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html

 Transform.rotation = Quaternion.LookAt(transform.forward, Vector3.up);

If you want it to smoothly rotate towards the direction of travel its a bit more work. You will have to cache the desired rotation and continuously lerp towards instead of setting it directly.

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
avatar image
0

Answer by uk007singh · Oct 18, 2017 at 09:19 AM

Hi Above script mentioned by you is working fine for me.I think you have to set the directions of the character. The character should face the z axis and x axis on the right and y axis to their upward direction.

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
avatar image
0

Answer by Harinezumi · Oct 18, 2017 at 10:24 AM

Hey there!

There are a few things I would change in your code as well as some suggestions for your player object.

First, there is a huge difference between transform.back and Vector3.back/left/right (which you use for everything except W): they are not aligned to your transforms rotation!

You also don't need a Rigidbody unless you want ragdoll effects at one point. The player character should be kinematic anyway.

Finally, a recommendation: put the visual representation of the player into a child game object of your player game object, so that you can rotate it separately from the player in case it is in a different coordinate system as Unity (usually that's the case).

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
avatar image
0

Answer by CreativeNess · Oct 19, 2017 at 03:55 PM

Thank you all for all your answers. I tried each one of them but none of them seemed to work (I tried the Quaternion and got errors, I moved my whole thing to have everything face the z axis but it continued to go right).
Until I decided to do something a little weird/dumb. I changed my W to Vector3.forward instead of transform.forward and it works perfectly fine now. When I hit my turn keys though it still rotates funny but that is something I can probably mess around with and fix another time. Thank you all though for your suggestions, I really appreciate it!

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

119 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

Related Questions

Jaggie, uneven speed with basic movement script. 0 Answers

Move in the direction its facing C# 2 Answers

Make the player face his movement direction 10 Answers

4 way direction movement using a Character Controller. 1 Answer

Character floats on backwards walk 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