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
0
Question by Vire · Jan 15, 2014 at 12:48 AM · movementcharactercontroller

Rotate Character towards Movement Direction

Might seem like a relatively simple thing but it's just not working for me, all the answers I've found online do not work.

I want my character to face the direction he moves when strafing, moving forwards, or backwards. Quaternion.LookRotation causes him to spin rapidly and I can't feed a transform.Rotate(moveDirection) into Y only when he needs to move for both X and Z

 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(CharacterController))]
 [RequireComponent(typeof(Rigidbody))]
 public class PlayerMovement : MonoBehaviour
 {
     public CharacterController characterController;
     public Animator anim;
     public Camera cam;

     public float moveSpeed = 5f;
     public float turnSpeed = 15f;
     public float gravity = 21f;
     
     private float h;
     private float v;
     private float s;
     private float mh;
     private float mv;
     private float deadZone = 0.1f;
     private Vector3 moveDirection = Vector3.zero;
     private Vector3 turnDirection = Vector3.zero;

     void Awake()
     {
         //need access to the character controller and animator
         anim = GetComponentInChildren<Animator> ();
         characterController = GetComponent<CharacterController> ();
         cam = Camera.main;
     }

     void Update()
     {
         //cache the inputs
         h = Input.GetAxis ("Horizontal");
         v = Input.GetAxis ("Vertical");
         s = Input.GetAxis ("Strafe");
         mh = Input.GetAxis ("Mouse X");
         mv = Input.GetAxis ("Mouse Y");

         moveDirection = Vector3.zero;

         //check if it's grounded, otherwise player cannot move, also check for the deadzone
         if(characterController.isGrounded)
         {
             ProcessVerticalMovement(v);
             ProcessHorizontalMovement(h, mh, s);

             //normalize the diagonal inputs to prevent values above 1
             if (moveDirection.magnitude > 1)
             {
                 moveDirection.Normalize ();
             }

             moveDirection = transform.TransformDirection(moveDirection);
         }



         //set the animator's Speed float to the magnitude of the moveDirection to allow for the run animation to play
         anim.SetFloat ("Speed", moveDirection.magnitude);

         //calculate gravity
         moveDirection.y -= gravity * Time.deltaTime;

         //move the character

         characterController.Move (moveDirection * moveSpeed * Time.deltaTime);
         transform.Rotate(turnDirection * Time.deltaTime);
         transform.Rotate (0f, moveDirection.x, 0f);
         //transform.rotation = Quaternion.LookRotation(moveDirection);
     }

     void ProcessVerticalMovement(float v)
     {
         if (v > deadZone || v < -deadZone)
         {
             moveDirection = new Vector3 (0f, 0f, v);

         }
     }
     
     void ProcessHorizontalMovement(float h, float mh, float s)
     {
         
         //if player is holding RMB while horizontal input then strafe
         if((s > deadZone || s < -deadZone) || (Input.GetButton("CameraTurn") && (h > deadZone || h < -deadZone)))
         {
             Debug.Log ("strafing");
             if(s > deadZone || s < -deadZone)
             {
                 moveDirection.x = s;
             }else
                 moveDirection.x = h;

         }
         //if player is pressing horizontal input then turn, or if player has RMB down (but not horizontal input) then turn
         if(Input.GetButton("CameraTurn") && (mh > deadZone || mh < -deadZone))
         {
             turnDirection = new Vector3(0f, mh * 200f, 0f);

         }else if(!Input.GetButtonDown("CameraTurn") && (h > deadZone || h < -deadZone))
         {
             turnDirection = new Vector3(0f, h * turnSpeed, 0f);

         }else
             turnDirection = Vector3.zero;

     }
 }
Comment
Add comment · Show 1
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 getyour411 · Jan 15, 2014 at 12:50 AM 0
Share

If he faces the direction and he's moving backward - isn't that forward?

1 Reply

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

Answer by gfoot · Jan 15, 2014 at 01:08 AM

The problem is probably that your user input is interpreted relative to the transform, and on the basis of that input you also update the transform's orientation - which means that on the next frame, the user's input is interpreted differently again. Unless the user input is straight up, it's going to spin on the spot really quickly.

If this really is the behaviour you want, but you want it to happen slower, then you could use Quaternion.LookRotation in the way you are now to work out which way you'd like the character to face, then use Quaternion.RotateTowards or similar to limit the rate at which the character actually turns. This means that an input that's slightly to the side will still cause the character to turn continuously, but at a much slower rate, and the player will have ample opportunity to control the turning and straighten up when the character has turned enough.

 Quaternion wanted_rotation = Quaternion.LookRotation(moveDirection);
 transform.rotation = Quaternion.RotateTowards(transform.rotation, wanted_rotation, MaxTurnSpeed * Time.deltaTime);

MaxTurnSpeed is measured in degrees per second - try starting with something like 200 and see how it feels.

Comment
Add comment · Show 2 · 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 Vire · Jan 15, 2014 at 01:17 AM 0
Share

That causes the character to rotate facing the ground (90 on x), then when I move him he spins all over the place.

avatar image Vire · Jan 15, 2014 at 03:17 AM 0
Share

I had to re-write my sidestep system but in the end I got it working using this method. Thanks

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

20 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

Related Questions

How to let a GameObject generate force but not be affected by certain forces? 0 Answers

SimpleMove not working on Y axis? 1 Answer

What is the best way to move a character for an FPS? 1 Answer

Footstep sounds system with a Vr touchpad movement system 1 Answer

Script Stopped Working - Walking on Walls 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