Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by Comicbook23 · Dec 12, 2021 at 01:11 AM · rotationmovementdirectionplayer movement

Character rotating to previous rotation

Hello! I'm pretty new to Unity scripting, but I wanted to start working on a sort of large project. I'm trying to make a third-person Pokemon-like game, where the camera can be rotated in whichever direction, and the player model will turn to face and move in that direction. I got the player model to look in the direction it's heading, but for some reason, as soon as you stop moving, the player model turns to look in the default direction it was when you start the game, if that makes sense. Does anyone know why that might be happening? Here is the code I have so far:

 public GameObject player;
 public CharacterController controller;

 public float playerSpeed = 10.0f;
 public float rotationSpeed;

 Vector3 move;

 private void Start()
 {
     controller = GetComponent<CharacterController>();
 }

 // Update is called once per frame
 void Update()
 {
     //Getting input from player - used for movement
     float x = Input.GetAxis("Horizontal");
     float z = Input.GetAxis("Vertical");

     //Player movement
     Vector3 movementDirection = new Vector3(x, 0, z);
     movementDirection.Normalize();
     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movementDirection), 0.15f);

     transform.Translate(movementDirection * playerSpeed * Time.deltaTime, Space.World);

     //Call sprint
     Sprint();
 }

 void Sprint()
 {
     //If left shift is pressed, double movement speed. Otherwise, keep it at the default speed
     if(Input.GetKeyDown(KeyCode.LeftShift))
     {
         playerSpeed = playerSpeed * 2;
     }
     else if(Input.GetKeyUp(KeyCode.LeftShift))
     {
         playerSpeed = 10.0f;
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by lluisvinent4 · Dec 12, 2021 at 02:17 AM

@Comicbook23 When you are not moving the player, the values ​​of Input.GetAxis are 0, since there is no input, therefore, x and z are also 0 and the player rotates in that direction. Check that x or z are not equal to 0 before set the rotation.

Comment
Add comment · Show 5 · 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 Comicbook23 · Dec 12, 2021 at 03:14 AM 0
Share

@lluisvinent4 Thank you! That seems to have fixed that problem. There seems to be a couple other problems, though. The character model isn't always turning to the direction it's moving. For example, when I move backwards, the model continues to face in whichever direction it's facing. If the model is facing in any direction other than the default forward direction, and I press W, it will simply move in the default forward direction without turning to face that way. Also, I'm hoping to have the model's "forward" direction be whichever way the camera is facing. Right now, it doesn't matter which way the camera is facing, the model just moves in the global "forward" direction. Is there a way I can change that?

avatar image lluisvinent4 Comicbook23 · Dec 12, 2021 at 01:27 PM 0
Share

@Comicbook23 If what you want is to make the direction relative to the rotation of the camera, you must multiply the rotation by the direction:

     void Update()
     {
         //Getting input from player - used for movement
         float x = Input.GetAxis("Horizontal");
         float z = Input.GetAxis("Vertical");
         if (x != 0 || z != 0)
         {
             //Player movement
             Vector3 movementDirection = new Vector3(x, 0, z);
             movementDirection.Normalize();
             Vector3 directionRelativeToCam = cam.transform.rotation * movementDirection;
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(directionRelativeToCam), 0.15f);
             transform.Translate(directionRelativeToCam * playerSpeed * Time.deltaTime, Space.World);
             //Call sprint
             Sprint();
         }
     }
avatar image Comicbook23 lluisvinent4 · Dec 12, 2021 at 11:07 PM 0
Share

That's basically done it! The only problem now is that the player can move downward. Like, if the camera is pointing down at all, the player will angle downward and try to move into the floor. Is there a way I can lock the player's z rotation to keep it from doing that? I think it would be the z rotation, but maybe it's y.

Also, the player will spin in a circle if I look to the side. I think it's almost there! If I look to the side, I think the player is constantly updating the direction it's supposed to turn, and since the camera continues to stay in the same spot relative to the player, that makes the player spin in a circle.

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

224 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

Related Questions

Rotate player in movement direction 1 Answer

How can I move an object to click point in 2D? 0 Answers

How to move relative to the orientation 1 Answer

How to walk in the direction the player is looking at in the Vive headset 1 Answer

Relative Movement Problem 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