Hello everyone,
i have been getting the error in the title and i dont know what to do. I was able to figure out that the script couldn’t find the CharacterController. But i dont know how to fix it. i have put the script off and on the player with the CharacterController but it doesn’t help.
this is the code im using
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float speed = 2f;
public float runSpeed = 6f;
public float jumpSpeed = 6f;
public float gravity = 20f;
private Vector3 moveDirection = Vector3.zero;
private int jumps;
private CharacterController controller;
void start()
{
controller = GetComponent<CharacterController> ();
}
void Update()
{
if (!controller) //Check for controller reference
{
Debug.LogError("Didn't find CharacterController");
}
if (controller.isGrounded) {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= speed;
if (Input.GetButtonDown ("Jump")) {
moveDirection.y = jumpSpeed;
}
jumps = 0;
if (Input.GetKey (KeyCode.LeftShift)) {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= runSpeed;
}
}
else {
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), moveDirection.y, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection.x *= speed;
moveDirection.z *= speed;
moveDirection.x *= runSpeed;
moveDirection.z *= runSpeed;
if (Input.GetButtonDown ("Jump") && jumps < 1) {
moveDirection.y = jumpSpeed;
jumps++;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
}