Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Help with CharacterController rotating down?

Discussion in 'Scripting' started by LinkUpGames, Jan 19, 2020.

  1. LinkUpGames

    LinkUpGames

    Joined:
    Dec 11, 2013
    Posts:
    19
    Hi I'm making a simple top-down 8-axis movement CharacterController that rotates in the Direction of movement. When the character is moving it works perfectly fine however when not moving the controller just rotates to be facing down towards the ground attached is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     private Transform rotatableTransform = null;
    10.  
    11.     public float speed = 6.0f;
    12.     public float gravity = 20.0f;
    13.  
    14.     private Vector3 moveDirection = Vector3.zero;
    15.  
    16.     CharacterController controller;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         controller = GetComponent<CharacterController>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.  
    28.         if (controller.isGrounded)
    29.         {
    30.             moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical"));
    31.             moveDirection = transform.TransformDirection(moveDirection);
    32.             moveDirection = moveDirection.normalized;
    33.             moveDirection *= speed;
    34.         }
    35.         moveDirection.y -= gravity * Time.deltaTime;
    36.         controller.Move(moveDirection * Time.deltaTime);
    37.  
    38.         rotatableTransform.rotation = Quaternion.Slerp(rotatableTransform.rotation, Quaternion.LookRotation(moveDirection), 0.15f);
    39.     }
    40. }
    This is what it does upon entering play mode:
     

    Attached Files:

  2. LinkUpGames

    LinkUpGames

    Joined:
    Dec 11, 2013
    Posts:
    19
    In case anyone happens upon this and has the same issue the fix was just moving the rotation calculation before all the movement calculations. Also placing it within an if statement to check the moveDirection wasn't zero.

    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.         moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical"));
    5.  
    6.         if(moveDirection != Vector3.zero)
    7.         {
    8.             rotatableTransform.rotation = Quaternion.Slerp(rotatableTransform.rotation, Quaternion.LookRotation(moveDirection), 0.15f);
    9.         }
    10.  
    11.         moveDirection = transform.TransformDirection(moveDirection);
    12.         moveDirection = moveDirection.normalized;
    13.         moveDirection *= speed;
    14.  
    15.         moveDirection.y -= gravity * Time.deltaTime;
    16.         controller.Move(moveDirection * Time.deltaTime);
    17.     }