Hi! I was wondering if anyone could help me get smoother and more realistic jumping in my project. I used the Brackeys tutorial to make it, but the jumping is a bit unrealistic. It floats down at the same speed the whole time. Any help? Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.8f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public float jumpHeight = 3f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.Raycast(groundCheck.position, Vector3.down, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float X = Input.GetAxisRaw(“Horizontal”);
float Z = Input.GetAxisRaw(“Vertical”);
Vector3 moveDirection = transform.right * X + transform.forward * Z;
controller.Move(moveDirection * speed * Time.deltaTime);
if(Input.GetButtonDown(“Jump”) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}