How would I go about adding jumping to this script?
(Note, the script is for Third person movement)
public CharacterController controller;
public float speed = 6f;
public float turnSmoothTime = 0.1f;
public float gravity = 9.8f;
//public bool isGrounded;
float turnSmoothVelocity;
public Transform cam;
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
//Debug.Log(controller.isGrounded);
if (!controller.isGrounded)
{
//isGrounded = false;
direction.y -= gravity * Time.deltaTime * 60;
controller.Move(direction * Time.deltaTime);
if (GetComponent<Transform>().position.y < -20f)
{
transform.position = new Vector3(5f, 5f, 5f);
}
}
if (direction.magnitude >= 0.1f && controller.isGrounded)
{
//isGrounded = true;
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(1f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}