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
16
Question by PaxForce · Oct 05, 2014 at 12:18 PM · movementcharactercontrollerdirection

Make the player face his movement direction

I've just made a few changes to my code:

 void Update () {
     
         ControllPlayer();
     }
 
 
     void ControllPlayer()
     {
         float moveHorizontal = Input.GetAxisRaw ("Horizontal");
         float moveVertical = Input.GetAxisRaw ("Vertical");
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         transform.rotation = Quaternion.LookRotation(movement);
 
 
         transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);
 
         
         if(Input.GetButtonDown ("Fire1"))
         {
             animation.Play ("attack-01");
         }
     }

Now the only thing that's wrong with it is, that when I stop pressing the movement keys on the gamepad, the Player turns automatically in the forward direction. I would like him to stay as he was when moving. Is there a quick fix to this?

Comment
Add comment · Show 6
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 · Oct 05, 2014 at 02:03 PM 0
Share

This line here:

     transform.rotation = Quaternion.LookRotation(_movement.normalized);

Is a pretty standard way of getting an object to face the move direction (don't need '.normalized'). How is it failing? If the rotation of your character is (0,0,0), is the front side facing positive 'z'?

avatar image KayelGee · Oct 06, 2014 at 12:05 PM 0
Share

Also is your model oriented correctly?

avatar image bubzy · Oct 10, 2014 at 09:07 AM 2
Share

how about

 if(moveHorizontal != 0 && moveVertical != 0)
 {
 transform.rotation = Quaternion.LookRotation(movement);
 }
avatar image ggMikael bubzy · Jan 30, 2017 at 11:19 PM 0
Share

Using this I cannot turn 360 degrees with the player, do you have the same issue?

avatar image PaxForce · Oct 10, 2014 at 10:30 AM 1
Share

thanks for the idea - I can't believe how easy this was to fix :) ...althought...you got one thing wrong. the correct way is this:

 if(moveHorizontal != 0 || moveVertical != 0)
 {
 transform.rotation = Quaternion.LookRotation(movement);
 }

avatar image bubzy · Oct 10, 2014 at 05:08 PM 0
Share

that's a matter of taste :D

10 Replies

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

Answer by PaxForce · Oct 10, 2014 at 04:03 PM

the problem was that I was moving in Space.Self instead of Space.World. fixed and done.

 float moveHorizontal = Input.GetAxisRaw ("Horizontal");
 float moveVertical = Input.GetAxisRaw ("Vertical");
  
 Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 transform.rotation = Quaternion.LookRotation(movement);
  
  
 transform.Translate (movement * movementSpeed * Time.deltaTime, Space.World);
Comment
Add comment · Show 6 · 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 Lhixan · Sep 14, 2017 at 01:10 PM 0
Share

How did you end up exactly fixing this? I'm still stuck with this problem and the code you posted here is the same as the one in your Question. I don't get how to set my player to move in space,world ins$$anonymous$$d of space.self

avatar image Dragondean · Sep 15, 2017 at 01:51 AM 0
Share

whenever i do this my character rotates 90 degrees along the Z axis does anyone have a fix??

avatar image roshanjafri31_unity · Mar 24, 2019 at 12:44 PM 0
Share

THAN$$anonymous$$S A LOT!!! it works like a charm :) however you could use the slerp to smooth it out.

avatar image ZLR_Games · Oct 20, 2019 at 04:46 PM 0
Share

Thanks dude! i had no idea you can use .LookRotation();, but now i do. This really helped me progress as a developer, i'm new.

avatar image RigorousSine759 · Dec 22, 2019 at 04:46 PM 0
Share

I have a problem, it tells me that "movementspeed " is not in the actual context. Im new btw

Show more comments
avatar image
27

Answer by hellopeople0004 · Sep 17, 2016 at 11:23 PM

Sorry for the necropost, but I am sure a lot of people are probably stumbling across this question. I myself a few hours ago found this question while searching for "How to make the player face movement direction" Now I noticed this question is already solved, and the answer works great. After doing a bit of research on Slerp, I made it so the script (that PaxForce made) will smoothly turn instead of doing a sharp turn when rotating.

The way it was done was by replacing transform.rotation = Quaternion.LookRotation(movement); with transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement), 0.15F);

If anyone wanted to know how it works, Quaternion.Slerp takes 3 arguments: 2 Quaternions and 1 Float. It interpolates the rotation between the 2 Quaternions with a speed of the Float value (0.0 is no movement while 1.0 is Instant Movement.) The code I did interpolates the rotation between the Current Rotation (transform.rotation) and the Movement Rotation (Quaternion.LookRotation) with a speed of 0.15F.

Hope this helped someone out there.

Comment
Add comment · Show 4 · 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 HumanSquid · Dec 09, 2016 at 10:35 PM 1
Share

I was looking all over the place for this solution. Works like a charm. Not sure why so many turning examples and tutorials didn't mention this.

avatar image Yousalc · May 27, 2018 at 11:51 PM 0
Share

Thank You so much This works perfectly

avatar image ZLR_Games · Oct 20, 2019 at 04:47 PM 0
Share

Thanks my dude! It's much better now. looks pretty smooth, especially when you turn 180 degrees.

avatar image kadusasso · Jun 25, 2021 at 03:13 PM 0
Share

Wow thanks , very usefull !

avatar image
10

Answer by ThreeYams · Sep 19, 2016 at 04:58 PM

@hellopeople0004 Your answer was right on time. Works perfectly.

Here's my end result. Nice and smooth.

if(movement != Vector3.zero) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement.normalized), 0.2f);

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 hellopeople0004 · Sep 20, 2016 at 04:16 PM 0
Share

No Problem, glad it helped :)

avatar image Shootingdutchtman · Aug 05, 2018 at 01:08 PM 0
Share

Thanks! I couldn't stop my object from rotating back to the default position.

avatar image mcaleerkj · May 28, 2021 at 02:06 AM 0
Share

This helped me not have the object rotate back to the default position as well. Thank you.

My camera was rotated, so in case someone also wants the code to work along a rotated camera, here is my full code:

     float moveHorizontal = Input.GetAxisRaw("Horizontal");
     float moveVertical = Input.GetAxisRaw("Vertical");
     Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
     Vector3 movementRotated = Quaternion.AngleAxis(Camera.main.transform.eulerAngles.y, Vector3.up) * movement;
     if (movement != Vector3.zero) transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movementRotated.normalized),

0.2f); transform.Translate(movementRotated movementSpeed Time.deltaTime, Space.World);

You can replace Camera.main.transform.eulerAngles.y with whatever angle you want (e.g. '30') depending on how your camera is rotated.

avatar image
3

Answer by daavid1231 · Oct 10, 2017 at 03:27 PM

@PaxForce just put this line transform.rotation =Quaternion.LookRotation(movement); into an if statement

 if (movement != Vector3.zero) {
         transform.rotation =Quaternion.LookRotation(movement);
     }

this works for me.

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
2

Answer by NickP_2 · Oct 06, 2014 at 12:56 PM

A way to handle this is to set the transforms forward to your normalized move direction, then when he has to walk, add a Vector3.forward force times the speed. For example:

 void Update()
 {
     Vector3 dir = <your movedirection (_movement)>.normalized;
     transform.forward = dir;
     transform.Translate(Vector3.forward * <speed (_movementspeed)> * Time.deltaTime)>);
 }

GL! :)

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 PaxForce · Oct 10, 2014 at 08:48 AM 0
Share

dude...did you try this piece code out? because it's not working for me.

  • 1
  • 2
  • ›

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

61 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

Related Questions

4 way direction movement using a Character Controller. 1 Answer

How to make character face towards movement direction? 4 Answers

Character floats on backwards walk 0 Answers

Move in the direction its facing C# 2 Answers

Jaggie, uneven speed with basic movement script. 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