- Home /
Raycast doesn't detect the ground when the player is on a ledge.
Hi, i'm fairly new to unity and C# programming overall and i am trying to make a basic physics based movement script but the problem i have now is that i'm trying to make my player jump by checking if he's grounded with a raycast. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
private bool grounded;
public float jumpforce;
private float distToGround;
public float thrust;
public Rigidbody rb;
public Transform PlayerCam;
public Collider PlayerCollider;
public float sensitivity;
private float xRotation;
private float desiredX;
void Start(){
rb = GetComponent<Rigidbody>();
distToGround = PlayerCollider.bounds.extents.y;
}
void Update(){
movement();
rotation();
jump();
GroundCheck();
}
void movement(){
if (Input.GetKey("z") && grounded == true)
{
rb.AddRelativeForce(Vector3.forward * thrust * Time.deltaTime);
print("z");
}
if (Input.GetKey("s") && grounded == true)
{
rb.AddRelativeForce(Vector3.back * thrust * Time.deltaTime);
print("s");
}
if (Input.GetKey("q") && grounded == true)
{
rb.AddRelativeForce(Vector3.left * thrust * Time.deltaTime);
print("q");
}
if (Input.GetKey("d") && grounded == true)
{
rb.AddRelativeForce(Vector3.right * thrust * Time.deltaTime);
print("d");
}
}
void rotation(){
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime;
Vector3 rot = transform.localRotation.eulerAngles;
desiredX = rot.y + mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
PlayerCam.transform.rotation = Quaternion.Euler(xRotation, desiredX, 0 );
transform.localRotation = Quaternion.Euler(0, desiredX, 0);
}
void GroundCheck(){
if (Physics.Raycast(transform.position, Vector3.down, distToGround + 0.1f)) {
grounded = true;
print("grounded");
} else {grounded = false; print("not grounded");}
}
void jump(){
if (Input.GetKeyDown("space") && grounded == true){
rb.AddForce(Vector3.up * jumpforce * Time.fixedDeltaTime);
}
}
}
But the problem i'm having currently is that when the player (currently a capsule) is standing on a ledge, the raycast won't detect that it's grounded because the collider isn't only in the middle. Maybe i should add more raycasts but i tought it was a good idea to ask for suggestions here. (If you have any other suggestions for my code it would also be appreciated).
this is a sreenshot of the problem:
Answer by Captain_Pineapple · Jan 29 at 11:36 PM
Well this is a fairly common issue that is not that easy to solve if you want to stick to raycasts. An easier way that will give you more consistent results would be to use the OnCollision
callbacks instead.
Check out this question here. The code in the question already implements the groundcheck using OnCollisionEnter
and OnCollisionExit
. You should be able to implement that in your own script.
Let me know if that helped.
Hi, thanks for responding to my question. Indeed, i saw it was also possible to do it like that but on an older question i saw someone saying that the OnCollisionExit method wasn't reliable and it's better to use raycasts instead. Is this still true or has this since been fixed ? Either way thanks for the response and i'll see if it works later today or next week.
well both methods have their issues. You already discovered the issue with raycasts. Solving this reliably is not that easy if you want to stick to raycasts. You can do a spherecast instead which would cover the complete surface of your current collider. This has the downside that it will get inaccurate if your character collider should ever become any more complex.
Doing things with OnCollision methods is reliable but you need to keep track of how many times the callbacks where called.
Imagine the following: you move from one ground collider onto another so that you are in contact with both at some point. Unity will tell you twice: "hey, you just collided with something that is tagged as ground". But then when leaving the one collider you are still on ground but you correctly get an OnCollisionExit
call. If you do not handle in your code that OnCollisionEnter
was called twice you would then say: "hey, i just left ground so i am in the air. So when leaving one Collider onto anotherone would prevent you from jumping unless you stand on the gap between 2 colliders again or drop of the collider onto different ground.
So basically while the solution i pointed out in general works, you are correct that it is not always as simple as that.
good luck, let us know how it went.
Oh , ok i understand what you mean. I'll test it out and tell you how it went when i do. Thanks a lot!
Your answer
Follow this Question
Related Questions
How to raycast a child collider of a parent rigidbody 2 Answers
How to rotate an object 360 degrees around a sphere with collisions? 0 Answers
isGrounded is always false, even with gravity, how do you fix that? 1 Answer
How to limit a ray to go downwards to infinity, how can I make it down only 1 float. 0 Answers
attached.Rigidbody isn't working with a ChactacterController? -1 Answers