- Home /
Phisycs2D.Linecast doesn't work correctly
Sorry for my poor English guys :D
I have some problem with this method. I tried to make the object jump. But "isGrounded" randomly sets to "true" when groundCheck is not in position of "Ground"-layer objects.
How can I fix this problem?
There is a code from Update:
var ballPos: Vector2 = new Vector2 (ballContainer.transform.position.x, ballContainer.transform.position.y);
var grCheckPos: Vector2 = new Vector2 (groundCheck.position.x, groundCheck.position.y);
isGrounded = Physics2D.Linecast(ballPos, grCheckPos, 1 << LayerMask.NameToLayer("Ground"));
if (isGrounded)
{
Debug.Log("Grounded");
jumpCheck = true;
}
if (!isGrounded)
{
jumpCheck = false;
}
Answer by Pyrian · Jun 23, 2014 at 05:26 PM
Wow, we so often see questions about collisions not occurring, it's almost refreshing to see a question about mystery collisions instead. It's also much easier to find out what's happening.
You could try something like:
RaycastHit2D GroundCheck = Physics2D.Linecast(ballPos, grCheckPos, 1 << LayerMask.NameToLayer("Ground"));
if (GroundCheck) Debug.Log("Ground hit " + GroundCheck.collider.name + " at " +
GroundCheck.point.ToString() + "(" + ballPos.ToString() + " - " +
grCheckPos.ToString() + ")");
Then you'll be able to see exactly what and where the Linecast is "randomly" intercepting something.
I may have the syntax off in that code snippet - you're using JavaScript and I'm more accustomed to C# - but the general idea should work.
Thanks for your reply, Pyrian :)
I tried this trick and that's what I get.
I've marked up debug message where GroundChecker sets to True incorrectly. (take a look at the Y-coordinate). I have no any objects in Ground-layer except this pillars.
But ball is colliding with this pillars somehow :( I have no idea what is it.
Well done! We've ruled out any problems with the positions, and it looks like the collision is in the expected area, and we know it's hitting a Pillar(Clone). $$anonymous$$y next thought is - are we sure we know which pillar it is hitting? $$anonymous$$aybe add a number to the name of each pillar as it is instantiated.
Also... Are you sure you can't divine any pattern in when this occurs? If this really is a bug, we'll want to replicate and report it.
Let's assume for a second that it's actually a bug in Linecast. Have you considered trying Raycast? It's really easy to switch back and forth. You could just use "ballPos", "grCheckPos - ballPos", and "(grCheckPos - ballPos).magnitude".
Yes, I tried to use it. Raycast is working the same way for me(
I've add the number to pillar and find out that ball hits distant fresh generated pillar O_O
Just take a look