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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
private Transform rotatableTransform = null;
public float speed = 6.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0.0f, Input.GetAxisRaw("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = moveDirection.normalized;
moveDirection *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
rotatableTransform.rotation = Quaternion.Slerp(rotatableTransform.rotation, Quaternion.LookRotation(moveDirection), 0.15f);
}
}
This is what it does upon entering play mode: