Hello,
I am trying to set up a third person character controller for non-humanoid characters. When I try to set up a third person character controller my character goes crazy when I press the w,a,s, and d keys. I have tried scripting my own character controller but to no avail. I have also tried replacing my character with the construction worker character but no animations play and the camera doesn’t work. Does anyone have any suggestions. Thank you.
Try this script. Add to your object Rigidbody and Character Controller.
public GameObject player;
// Direction of movement
Vector3 speedUp;
Vector3 speedDown;
Vector3 speedLeft;
Vector3 speedRight;
// For making code easier to read
Rigidbody playerGetRigidbody;
float playerSpeed = 3.0f;
void Start () {
playerGetRigidbody = player.GetComponent<Rigidbody>();
speedUp = new Vector3( 0, 0, 3);
speedDown = new Vector3( 0, 0,-3);
speedLeft = new Vector3(-3, 0, 0);
speedRight = new Vector3( 3, 0, 0);
}
void Update () {
// Move Up
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
playerGetRigidbody.MovePosition(
playerGetRigidbody.position + speedUp * Time.deltaTime * playerSpeed);
// Move Down
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
playerGetRigidbody.MovePosition(
playerGetRigidbody.position + speedDown * Time.deltaTime * playerSpeed);
// Move left
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
playerGetRigidbody.MovePosition(
playerGetRigidbody.position + speedLeft * Time.deltaTime * playerSpeed);
// Move right
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
playerGetRigidbody.MovePosition(
playerGetRigidbody.position + speedRight * Time.deltaTime * playerSpeed);
}