I am making a player movement script using character controller and my velocity.y keep increasing after hitting the ground ,How to reset my velocity to 0 in vector3 , my player velocity.y keep increasing even i hit a ground and is not resetting to 0f . help pls
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//character controller
public CharacterController controller;
//player movement Float
public float speed = 12f;
//player gravity Float
public float gravity = -19.62f;
//player distance between ground Float
public float groundDistance = 0.4f;
//transform for ground checking
public Transform groundCheck;
//to let it know what object to check for
public LayerMask groundMask;
//variables velocity
private Vector3 velocity;
private bool isGrounded;
// Update is called once per frame
void Update()
{
//this part is configuring the ground check and if is not grounded
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = 0f;
}
//movement controller
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
//gravity and velocity configure
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}