- Home /
 
How can I stop player snapping back to 0,0,0 rotation when I release keys?
My player moves with my keys properly, but when I release the keys he snaps back to pointing directly upwards. How can I stop this?
(Script placed on cube with Character Controller):
 using UnityEngine;
 using System.Collections.Generic;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour
 {
 
     public float speed = 9.0F;
     public float gravity = 13.0F;
 
     private Vector3 moveDirection = Vector3.zero;
     public CharacterController controller;
 
     void Start()
     {
         // Store reference to attached component
         controller = GetComponent<CharacterController>();
     }
 
     void Update()
     {
 
         // Character is on ground (built-in functionality of Character Controller)
         if (controller.isGrounded)
         {
 
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
             moveDirection = transform.TransformDirection(moveDirection);
             transform.GetChild(0).rotation = Quaternion.LookRotation(moveDirection);
 
             moveDirection *= speed;
 
         }
 
        //GRAVITYYY
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);
 
     }
 }
 
              Answer by Tsaras · Mar 28, 2019 at 06:32 AM
Your rotation is resetting because you always assign it a value based on user input. What you need to do is have the character rotate only when moving. Line transform.GetChild(0).rotation = Quaternion.LookRotation(moveDirection); needs to be in some sort of if statement, depending on how you want this to behave.
Try the following and you will get the idea:
 if ((moveDirection.x != 0) || (moveDirection.z != 0))
 {
     transform.GetChild(0).rotation = Quaternion.LookRotation(moveDirection);
 }
 
              This works diagonally but unfortunately it makes the character walk backwards sometimes when moving horizontally or vertically.
Thanks for that! That's working so I've marked it as correct - though it snaps my characters to 90degree intervals (ie. only horizontal or vertical) when I release keys, and only very occasionally 45 degrees (diagonal). No worries if you don't know how I'd do that - you've helped heaps!
Your answer
 
             Follow this Question
Related Questions
Character Controller problem 0 Answers
rotate around character 1 Answer
CharacterController NullReferenceException dispite attached to object 1 Answer
rotate around character 2 Answers
Character controller wont move 1 Answer