Hello everyone,
I am working on an endless runner based on N3K EN’s endless runner tutorials on Youtube. However, the tutorial does not explain how to add a jump feature using the Character Controller component in Unity.
Could anyone tell me how to add a jumping system?
Thanks in advance.
The current script is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBehaviour : MonoBehaviour
{
private CharacterController controller;
public float speed = 5.0f;
private Vector3 moveVector;
private float verticalVelocity = 0.0f;
private float gravity = 12.0f;
private float animationDuration = 1.0f;
public float jumpower = 5.0f;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Time.time < animationDuration)
{
controller.Move(Vector3.forward * speed * Time.deltaTime);
return;
}
moveVector = Vector3.zero;
if (controller.isGrounded)
{
verticalVelocity = -0.5f;
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
moveVector.x = Input.GetAxisRaw("Horizontal") * speed;
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
}