using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
public CharacterController cc;
public float gravity = -9.81f;
public float gravityScale = 1;
public float jumpHeight = 4;
public float groundDistance = 0.4f;
Vector3 velocity;
public Transform groundCheck;
public LayerMask groundMask;
bool isGrounded;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetKeyDown(KeyCode.Space))
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * (gravity * gravityScale));
}
velocity.y += gravity * gravityScale * Time.deltaTime;
MovePlayer();
}
void MovePlayer()
{
cc.Move(new Vector3(0, velocity.y, 0) * Time.deltaTime);
}
}
if(!isGrounded && velocity.y > 0)
{
if (Input.GetKeyDown(KeyCode.Space))
{
velocity.y = 0f;
}
}
Something kind of like that.