Hey, Unity users! This should be a very easy fix, but I’m a novice programmer in C# and I don’t know what I can do. I wrote this script to make a third person character controller. The idea is that the controller would move forward in the direction the camera is facing. The camera (which is attached to the script in the inspector) does not move (yet). I really feel like my script should work, however it just doesn’t. When I start up the game, 9/10 times, nothing happens but the game is running. And when it does move, the controls are weird or shakey. Can someone lend me a hand? I would really appreciate it! Here is the script:
public class MovementScript : MonoBehaviour
{
public float movementSpeed = 6.0F;
public float levelGravity = 20.0F;
private Vector3 movementForce = Vector3.zero;
public Transform cameraMovement;
public CharacterController characterController;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
if (characterController.isGrounded)
{
if (Input.GetKey(KeyCode.W))
{
movementForce = cameraMovement.TransformDirection(movementForce);
}
if (Input.GetKey(KeyCode.D))
{
movementForce = transform.position += Vector3.right * movementSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
movementForce = transform.position += Vector3.left * movementSpeed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.S))
{
movementForce = transform.position += Vector3.down * movementSpeed * Time.deltaTime;
}
}
characterController.Move(movementForce * Time.deltaTime);
movementForce *= movementSpeed;
}
}