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);
}
}