Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

c# - Unity Physics2D.Raycast hits itself

I'm trying to use Physics2D.Raycast in order to check if the player is on the ground (I know that there are other approaches to check if the player is on the ground but I think that the raycast is the most reliable). The problem is that in my scenario it returns the player itself as hit and I really don't understand why and what should I do. My code (in PlayerController) is the following:

 public bool IsGrounded () {
        Bounds bounds = this.playerCollider.bounds;
        Vector3 rayOrigin = bounds.center;
        rayOrigin.y -= bounds.extents.y;
        RaycastHit2D hit = Physics2D.Raycast (rayOrigin, Vector2.down, 0.1f);
        if (hit.collider != null) {
            Debug.Log ("Collider is: " + hit.collider.name);
        }
        return hit.collider != null;
    }

And I can debug the casted ray using:

Debug.DrawLine (rayOrigin, new Vector3 (rayOrigin.x, rayOrigin.y - 0.1f, rayOrigin.z), Color.magenta);

...and it gets casted as expected, while the Debug.Log always reports "Player" which is itself and I don't know how it's possible. So what's wrong?

ps. I'm using Unity 5.3

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is occurring because your Player is overlapping at the start of the raycast. There are few ways to fix this:

1.Disable Queries Start In Colliders.

Go to Edit->Project Settings->Physics 2D then make sure that Queries Start In Colliders is NOT checked. Your code ran fine after changing that. Here is a screenshot:

enter image description here

2.Another solution is to use layers.Raycasting but ignoring the Player layer.Before you do that, make sure to create a layer called Ground and put your ground GameObject to the Ground layer then create another layer called Player and put your player in the Player layer. We can now use bitwise operator to exclude Player layer from the raycast.

enter image description here

Now, lets assume that Player layer number is 9. The code below should fix your problem.

public int playerLayer = 9;
int layerMask = ~(1 << playerLayer); //Exclude layer 9
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.down, 0.1f, layerMask);

That's it. Both of these were able to solve your problem.

For other people reading, below are other ways to easily detect when Player is touching the floor without using Physics2D.Raycast or doing all those things above.

Simply attach to the Player.

public class Player : MonoBehaviour
{
    public LayerMask groundLayer;
    Collider2D playerCollider;
    bool grounded;

    void Start()
    {
        playerCollider = gameObject.GetComponent<Collider2D>();
    }

    public bool IsGrounded()
    {
        grounded = Physics2D.OverlapCircle(playerCollider.transform.position, 1, groundLayer);
        return grounded;
    }
}

Or you can use IsTouchingLayers.

public bool IsGrounded()
{
    grounded = grounded = playerCollider.IsTouchingLayers(groundLayer.value);
    return grounded;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...