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 UltraGearGames · Feb 09, 2019 at 07:54 AM · rotationmovementdirectionplayer movement

Rotate player in movement direction

Help please. Im trying to get my player to rotate to the direction he is supposed to go to. It works for W and S but A and D does not rotate, yet still moves. How to solve this? please tell me, i actually have no idea.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewPlayerController : MonoBehaviour {
 
     public float speed;
     public float jumpVelocity;
     public bool doubleJumpAvaible;
     public Animator anim;
     public bool isGrounded;
     public bool canMove;
     Rigidbody rb;
 
 
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
 
 
     void Update()
     {
 
     }
 
 
 
     void FixedUpdate()
     {
         //Player movement
         if (canMove)
         {
             if (Input.GetKey(KeyCode.W))
             {
                 transform.rotation = new Quaternion(transform.rotation.x, 0, transform.rotation.z, transform.rotation.w);
                 transform.Translate(0, 0, speed * Time.deltaTime);
             }
 
             if (Input.GetKey(KeyCode.A))
             {
                 transform.rotation = new Quaternion(transform.rotation.x, -90, transform.rotation.z, transform.rotation.w);                
                 transform.Translate(0, 0, speed * Time.deltaTime);
             }
 
             if (Input.GetKey(KeyCode.D))
             {
                 transform.rotation = new Quaternion(transform.rotation.x, 90, transform.rotation.z, transform.rotation.w);
                 transform.Translate(0, 0, speed * Time.deltaTime);
             }
 
             if (Input.GetKey(KeyCode.S))
             {
                 transform.rotation = new Quaternion(transform.rotation.x, -180, transform.rotation.z, transform.rotation.w);
                 transform.Translate(0, 0, speed * Time.deltaTime);
             }
         }
 
 
 
     }
 
 
 
 
 
 }
 


any help is appreciated.

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
Best Answer

Answer by Chimer0s · Feb 09, 2019 at 11:22 AM

If you're going to set the values manually like that, you may as well just use euler angles and save yourself the headache of using quaternions.

   using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
   
   public class NewPlayerController : MonoBehaviour {
   
       public float speed;
       public float jumpVelocity;
       public bool doubleJumpAvaible;
       public Animator anim;
       public bool isGrounded;
       public bool canMove;
       Rigidbody rb;
   
   
   
       void Start()
       {
           rb = GetComponent<Rigidbody>();
       }
    
   
       void FixedUpdate()
       {
           //Player movement
           if (canMove)
           {
               if (Input.GetKey(KeyCode.W))
               {
                   transform.eulerAngles = new Vector3(transform.eulerAngles.x, 0, transform.eulerAngles.z);
                   rb.velocity = speed * transform.forward);
               }
   
               if (Input.GetKey(KeyCode.A))
               {
                   transform.eulerAngles = new Vector3(transform.eulerAngles.x, -90, transform.eulerAngles.z);                
                   rb.velocity = speed * transform.forward);
               }
   
               if (Input.GetKey(KeyCode.D))
               {
                   transform.eulerAngles = new Vector3(transform.eulerAngles.x, 90, transform.eulerAngles.z); 
                  rb.velocity = speed * transform.forward);
               }
   
               if (Input.GetKey(KeyCode.S))
               {
                   transform.eulerAngles = new Vector3(transform.eulerAngles.x, 180, transform.eulerAngles.z); 
                   rb.velocity = speed * transform.forward);
               }
           }
   
   
   
       }
   
   
   
   
   
   }
  

I took the liberty of removing your update function as it shouldn't be there if it isn't doing anything. More importantly, I also switched your movement to be handled by giving your rigidbody velocity. You did well to handle the movement in FixedUpdate, but when moving a rigidbody you never want to move it by its transform. Doing so will prevent the physics engine from calculating proper collisions with it.

This should work well enough, but I'd suggest looking into lerping as a means of rotating the character to make the transition a bit more smooth. Good luck with your project!

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 UltraGearGames · Feb 14, 2019 at 09:59 PM 0
Share

Wow, thank you! also Wow, my questions dont get stuck forever in moderation anymore!

avatar image UltraGearGames · Feb 14, 2019 at 10:05 PM 0
Share

I have actually also gave up on the project(it's a 3D platformer) and i tried making a 2D platformer one, but i seem to get too lazy to work on anything. When i think about going back, i think about that making a 2D platformer would be amazing too and easier, but i wanna come back at the same time... any tips?

avatar image UltraGearGames · Feb 15, 2019 at 02:46 PM 0
Share

Also, the reason i did not use rigidbody movement was because i was afraid from my past experience that the player would feel terrible, unresponsive and uninteresting to control. Any tips on that?

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

177 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

Related Questions

Character rotating to previous rotation 1 Answer

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

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

How to move relative to the orientation 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