I'm trying to implement a jump method into my FPSInput script. I know how to make this work for a Rigidbody, but how can I accomplish this with my Character Controller? Does CharacterController have a method comparable to AddForce?
First Person Movement Input:
public class FPSInput : MonoBehaviour {
public float speed = 6.0f;
public float gravity = -9.8f;
private CharacterController _charController;
void Start() {
_charController = GetComponent<CharacterController>();
}
void Update() {
float deltaX = Input.GetAxis("Horizontal") * speed;
float deltaZ = Input.GetAxis("Vertical") * speed;
Vector3 movement = new Vector3(deltaX, 0, deltaZ);
movement = Vector3.ClampMagnitude(movement, speed);
movement.y = gravity;
movement *= Time.deltaTime;
movement = transform.TransformDirection(movement);
_charController.Move(movement);
}
}
Works for Rigidbody only:
public class Jump : MonoBehaviour {
public Vector3 jumpVector;
// Update is called once per frame
void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.VelocityChange);
}
}
}