简体   繁体   中英

Unity 2D : Collisions detection

I am trying to make a Pokemon Game and I have a problem with the Collision Detection. For example, if I want to turn right here:

试图走对

I should be able to do it, but I can not move. Both objects have BoxCollider2D. This is the BoxCollider of the player:

玩家的BoxCollider

The tree has a boxcollider with size 1, 1 and offset 0, 0.

And here is my code:

    void Update()
{
    input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    actualSpeed = Input.GetKey(KeyCode.LeftShift) ? walkingVelocity : runingVelocity;

    if (input != Vector2.zero && p == transform.position)
    {
        anim.SetBool("isMoving", true);

        if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
        {
            if (input.x > 0)
            {
                if (direction.Equals(Direction.Este) && canMove())
                {
                    p += Vector3.right;
                }else
                {
                    direction = Direction.Este;
                }                   
            }
            else
            {
                if (direction.Equals(Direction.Oeste) && canMove()) {
                    p -= Vector3.right;
                }
                else{
                    direction = Direction.Oeste;
                }
            }
        }
        else
        {
            if (input.y > 0)
            {
                if (direction.Equals(Direction.Norte) && canMove())
                {
                    p += Vector3.up;
                }else
                {
                    direction = Direction.Norte;
                }

            }
            else
            {
                if (direction.Equals(Direction.Sur) && canMove())
                {
                    p -= Vector3.up;
                }
                else
                {
                    direction = Direction.Sur;
                }

            }
        }
        anim.SetFloat("input_x", input.x);
        anim.SetFloat("input_y", input.y);

    }
    else if (input == Vector2.zero)
    {
        anim.SetBool("isMoving", false);
    }
    transform.position = Vector3.MoveTowards(transform.position, p, actualSpeed * Time.deltaTime);
}

bool canMove()
{
    bool b = true;
    Ray2D r;
    if (direction.Equals(Direction.Norte))
    {
        r = new Ray2D(transform.position, Vector3.up);
    }
    else if (direction.Equals(Direction.Sur))
    {
        r = new Ray2D(transform.position, Vector3.down);
    }
    else if (direction.Equals(Direction.Este))
    {
        r = new Ray2D(transform.position, Vector3.right);
    }
    else
    {
        r = new Ray2D(transform.position, Vector3.left);
    }

    Debug.DrawRay(r.origin, r.direction);

    RaycastHit2D hit = Physics2D.Raycast(r.origin, r.direction, 1f , 1 << 8);
    if (hit.collider != null)
    {
        if (hit.collider.CompareTag("Obstacle"))
        {
            b = false;
        }
    }
    return b;
}

How can I make the player to move above the tree?

Thanks! PD. I'm pretty new with Unity2D

This Problem is caused by the friction between the two colliders, You should create a Physics Material 2D and set its friction to 0 and add it to the Box Collider 2D material.

Also consider using Circle Collider instead of box on the obstacle(tree) since it is circular.

The problem you have may be caused, by the borders of the gameObjects, which may be still intersecting. One way to solve this is to set the size of the rigbody to (0.9, 0.9), instead of (1, 1), but then you need to add an additional script which checks wether you can do a move without running in an object.

Since you have a tile based game (just like in pokemon i guess), this should be a good way to do it. When you aren't that deep in the unity engine you should check out the beginner tutorials on their website. Here is a Link to the Tutorial of Roguelike, which contains the exact same movement as you may want to have. Everything is explained pretty well and you can copy all the scripts their writing in the tutorials ;) just watch it maybe you will find new things that you haven't known yet.

Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM