- Home /
Physics2D.Raycast.collider is always returning an object?
Hey, guys!
So I've ran into a problem. I'm trying to make it to where a player cannot infinitely jump in the air, so I've ran through some tutorials on how to achieve this, and this is what I have so far.
void Jump() {
if (isGrounded.collider == null) {
return;
}
rigidbody2D.AddForce (new Vector2(0, jumpHeight), ForceMode2D.Force);
}
void FixedUpdate() {
isGrounded = Physics2D.Raycast (transform.position, -Vector2.up, .5f);
Debug.Log (isGrounded);
//Debug.Log (transform.position);
}
The function Jump() is called from the Update function. So basically what I've done is check whether isGrounded.collider is null, for that would mean I'm in the air (or that's what I think that's what it's achieving). If that's the case, simply return nothing without actually jumping. However, when I'm actually in the game, isGrounded.collider is always returning something, regardless of whether I'm actually grounded or not. I've tried everything (lowering distance, using different functions, etc.), but nothing's working. Please help!
Or is there a better way to achieve what I'm going for?
Answer by Hanarchist · Jul 03, 2014 at 05:40 PM
Okay, I found a solution. Instead of using Physics2D, I chose to go with Physics instead, and things seem to be working now (changed code accordingly, and with appropriate Box Colliders and Rigidbodies).
Thanks for all the people who took the time to help!
Answer by tanoshimi · Jul 03, 2014 at 04:13 PM
Instead of:
isGrounded = Physics2D.Raycast (transform.position, -Vector2.up, .5f);
Debug.Log (isGrounded);
Try:
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, .5f);
if (hit.collider != null) {
Debug.Log (isGrounded);
}
Thanks for the help! This method didn't quite help, and I still achieve the effect of infinite jumping. isGrounded is still showing up to be true for all cases.
What's the scale of your game world? Try setting .5f to be 0.001f ins$$anonymous$$d.
I don't think the distance is the issue. I've already tried setting the distance to be (something like) 0.00000000000000001f, and it still read to be true.
Jumping off the side to where I ideally would not be touching any collider still shows isGrounded to be true.
Am I misunderstanding something about Physics2D.Raycast?
By the way, am I correct in interpreting your
if (hit.collider != null) {
Debug.Log (isGrounded);
}
to actually meaning
if (hit.collider != null) {
isGrounded = true;
}
?
Answer by robertbu · Jul 03, 2014 at 04:13 PM
You want to check the collider or the transform:
isGrounded = (Physics2D.Raycast (transform.position, -Vector2.up, .5f).collider != null);
The RaycastHit2D returned by Physics2D is a struct and will never be null.
Thanks for the answer! I've tried this method, and changed everything accordingly, but it's still showing isGrounded to be true for all cases.
Answer by oct · Jul 04, 2014 at 11:20 AM
check to see if the ray from transform.position is not hitting some child (or something) of the object itself. use Debug.DrawLine to visualy see the ray
Your answer
Follow this Question
Related Questions
Raycast 2D hits sprite with collider, but returns error when not hitting any collider 2 Answers
How to rotate a player around the corner of a box? 0 Answers
Applying damage with Flamethrower-like weapon 1 Answer
Can't get a laser working properly. 2 Answers
How can I make diagonal collisions work with raycasts from a boxcollider? (3D, C#) 0 Answers