I am a beginner in c# and Unity , and I want to use CharacterController.Move script from the documentation.
there is a simple scene and I just drop this script in “player”, but it doesn’t work
there is a controller script
I am a beginner in c# and Unity , and I want to use CharacterController.Move script from the documentation.
there is a simple scene and I just drop this script in “player”, but it doesn’t work
It is because you are missing the component ‘CharacterController’ component.
To add it do this: In the Inspector on the bottom of it you will see a button that says ‘Add Component’ => Press it => In the search bar at the top type ‘CharacterController’ and add it.
Otherwise just use the script I sent and then add it to your player and it should work. Cheers mate.
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class Controller : MonoBehaviour {
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump") {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}