- Home /
Question by
maleficientmatter · Oct 01, 2021 at 10:08 AM ·
jumpingground detection
ground check stops working after one jump,im making an fps and i added a movement scrip and a ground checker script, if i press the jump button and then fall, the ground checker stops working.
i am making a 3d fps game and the ground check stops working if i jump and then fall, i cant jump again because the groundcheck wont activate again
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class movement : MonoBehaviour { public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundChecker;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundChecker.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -1f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * 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);
}
} ,basically, if i jump the ground checker just stops and i cant figure out why. here is the 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.81f;
public float jumpHeight = 3f;
public Transform groundChecker;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundChecker.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -1f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * 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);
}
}
Comment
Your answer