- Home /
Using raycast to determine if player is grounded.
So I've been trying to use Physics,Raycast without much luck at all. I've got a small empty Game object affixed right below the player, which is supposed to send a ray downwards and tell if it's colliding with anything. When I initially run the code, for a brief moment, it returns "is grounded", then switches to "is not grounded" and never switches back, whether I jump or not. I'm especially confused because I feel like I'm doing everything right, and when I switch the direction of the ray to up (the direction of the player), it comes back as true. It's only when sending it downwards to the BoxCollider I'm using as ground does it not see it.
Here's my code
void Update ()
{
if (Physics.Raycast(transform.position, transform.TransformDirection (new Vector3(0,-1,0)), .1f, mask))
{
Debug.Log ("Is grounded");
}
else
{
Debug.Log ("Is not grounded");
}
And as far as the direction, I've tried manually inputting the Vector3, I've tried using -Vector3.up, and I've tried assigning Vector3.up to a variable, and using the negative of that variable. None of it works.
I see three potential issues here, but not are obviously the problem you are having. First you are using transform.TransformDirection(). This is taking the local down of your game object and getting a world direction. So if your object is not perfectly aligned with the world 'Y' or if your model is not constructed with 'Y' as up, the Raycast() will fail. Typically I see this code with Vector3.up (no Transform.TransformDirection()) so that the cast is the world down. Sometimes it uses the character down for walking on terrain, but I don't think that is what you are doing.
The second potential issue is the 'mask'. I don't have the value you use, but not understanding the 'mask' parameter is common problems in questions posted to the list.
Lastly, this code depends on your placement of the empty game object. If you are just a bit high, you would get the behavior you describe. I would suggest using the character pivot and just having the distance be slightly more than the distance of the pivot from the bottom of the character. Since mesh is one-sided, you don't have to worry about the cast hitting the character.
Yeah, transform.Transform direction is just the newest in numerous permutations I've tried. I just switched it to Vector3.down to no avail, and the same with -Vector3.up
The mask was just a Layer$$anonymous$$ask I named, so I could mess with it in the editor. A holdover from when I was trying to cast from within the sphere. Even when the Layer$$anonymous$$ask is changed, or removed entirely, I get the same effect.
The placement of the object doesn't seem to be helping either. I've actually tried placing the game object parallel on the X axis, just to see if that had an effect, with nothing possibly obstructing the ray other than the BoxCollider, and again, no luck.
I'm utterly stumped.
Edit:
Okay, here's a new development:
I again tried placing the gameobject parallel on the X axis, -5 units away, and made the Ray 5 units long, projected down, because I'm just throwing stuff at the wall at this point.
Is Grounded is co$$anonymous$$g out true now. He's the kicker: If I move far enough that the gameObject is above empty space, isGrounded becomes false. When I move back over solid ground, it remains false.
Why would this not be updating? Am I misunderstanding how Physics.Raycast works? Is it not a continual effect? I assumed since it was in Update (Or fixedUpdate, I've tried both) it would be called once per frame, but it doesn't seem to be updating.
Yes you can raycast out of a sphere. I'm not sure what is going on. Sometimes when I see issues like yours, the colliders are messed up...not aligned with the object or missing. I would recommend starting with something very simple. Start a new scene. $$anonymous$$ake the ground a really big box. $$anonymous$$ake your character a box, then create the simplest raycast you can (no mask). Use Vector3.down for the direction. Then test your code. This way you can fix any script issue so the issues when you use your current scene will be collider/game object issues.
Oh my god.
So it seems something was messed up with one of the colliders, either the player avatar or the ground. Cant say which, deleted both and remade them. Everything works now. I'm really glad because I was starting to think I was stupid. $$anonymous$$y code was working fine, that's good to know.
A million billion thanks.
Answer by Crystalline · Oct 26, 2013 at 06:55 AM
Attach it to your player colider .
var distToGround: float;
function Start () {
distToGround = collider.bounds.extents.y;
}
function IsGrounded(): boolean
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
function FixedUpdate () {
if (IsGrounded())
{
}
So I've fiddled with those for a day or so now and no luck. Is it because my character is a sphere? As I understand it, I can't raycast out of a sphere.
Then create a "dummy" character controller physis type , only to use it for raycast.
Your answer
Follow this Question
Related Questions
Raycast hit in OnDrawGizmos but not in Update 1 Answer
Why won't Physics.Raycast return false after it's first return of true? 1 Answer
Trying to figure out if one object can see another 1 Answer
Raycast help: casting 3 rays, out of the faces of a cube 2 Answers
Rays do not detect terrain and prefabs do not spawn where wanted 1 Answer