- Home /
[i know it has been asked but my problem is different can't find answers for that] Help With Ground Detection, Using BoxCollider Raycast method not working like i want
hello guys, i am trying to make my character jump which is basically a box right now jump is working but if i keep pressing space key character keeps jumping in mid air i don't want that. i know it can be solved by ground detection i am RayCast for that, problem with that is if ground is uneven ( slop like) jump stops working because of that i need a efficient way for detecting ground. there is a thing "CheckBox" i don't know how to use it i would be great if anyone tell how can i detect ground using it and explain a bit. please help and thank you in advance
My Code for now
public class PlayerJump : MonoBehaviour
{
public float JumpForce;
public float ForwardMove_Jump;
public float distToGrorund;
public bool Jump;
private BoxCollider col;
private CharacterControl characterControl;
private void Awake()
{
characterControl = GetComponent<CharacterControl>();
col = GetComponent<BoxCollider>();
distToGrorund = col.bounds.extents.y;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Jump = true;
}
else
{
Jump = false;
}
}
private void FixedUpdate()
{
if (Jump == true && isGrounded())
{
characterControl.RIGIDBODY.velocity = transform.TransformDirection(0, JumpForce, ForwardMove_Jump) * Time.deltaTime;
}
}
private bool isGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, distToGrorund + 0.1f);
}
}
Answer by tormentoarmagedoom · Sep 24, 2019 at 02:49 PM
Hello there!
Most easy solution is to tag all ground objects as "ground". Then create a box collider (lets cal itl detector) marked as trige, under your character, and use the Unity function OnTriggerStay() in this detector.
This way, you will know each frame is this Detector is colliding with other colliders. So, you only need to check if any of this other colliders is tagged "ground".
So if you have in your character FixedUpdate a command to set "grounded" bool variable to false, and then in your OnTriggerStay() a "if sentence" detecting other collider tagged as ground, make this grounded variable true.
Something like this (not code, just explainign logic)
FixedUpdate()
{
Grounded = false
}
OnTriggerStay(collider other)
{
if (other.tag == "ground")
{
grounded = true
}
}
This way, you will know every moment if its or not grounded, so you can allow or not to jump.
There are several youtube tutorials about this. If the detector does not work well in slopes, make your detector have another size, or move it a little, or make more than 1 collider to have all "areas" covered.
Good luck!
Can you please give men link for one the video that follow this step
Look for them, spend your time. If pretend to learn quick ,you will learn nothing. If go slowly and watching videos you will finnally learn things you didnt expected more usefull that what you where looking for.
good luck my friend! :D
i was looking for the solution from more than a week now can't find anything, ,but thank you i searched your way alot and found a question on this site and fixed it