Top Down Colliders Issue

I’m making a top down dungeon crawler and I currently have a player set up with a rigidbody2d and box collider 2d. However, my character can walk over other colliders (walls, chests, etc) in the scene. The character moves one unit block each time a key is pressed (in this case 32 units and all sprites are 32x32 including the character). What is ignoring colliders in my script and how can I make boundaries actually happen?

Player Controller:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public Sprite playerNorth;
    public Sprite playerEast;
    public Sprite playerSouth;
    public Sprite playerWest;
    public GameObject chest;
    public Sprite chestOpen;

    private SpriteRenderer rend;
    private float speed = 1.0f;
    private Vector3 pos;
    private Transform tr;

    void Start ()
    {
        pos = transform.position;
        tr = transform;

        rend = GetComponent<SpriteRenderer>();

        if(rend.sprite == null)
        {
            rend.sprite = playerNorth;
        }
    }

    void Update ()
    {
        OnMove();
    }

    void OnMove()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            rend.sprite = playerWest;

            pos += Vector3.left;
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            rend.sprite = playerEast;

            pos += Vector3.right;
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            rend.sprite = playerNorth;

            pos += Vector3.up;
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            rend.sprite = playerSouth;

            pos += Vector3.down;
        }

        transform.position = Vector3.MoveTowards(transform.position, pos, speed);
    }

    void OnCollisionEnter2D (Collision2D other)
    {
        if (other.gameObject.tag == "Chest")
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                SpriteRenderer cOpen = chest.GetComponent<SpriteRenderer>();
                cOpen.sprite = chestOpen;
            }
        }
    }
}

You’re overriding the transform position so the rigidbody can’t control it and enforce collision reactions. Use the rigidbody’s interface to move the character with something like Rigidbody2D.MovePosition instead of `pos +=Vector3.up;’ which directly affects the transform.position.

Thank you! However, now the character only moves one position (for instance, pressing A only moves the character to (-1,0)). I have a chest sitting at (-1,1) and if I am at (-1,0) and press W, it tries to move to (0,1) but due to the collider of the chest, it moves to like (0,0.5688).

Grid-based games should not use physics, but use an array to represent the world and base everything off that.

–Eric

Regardless of the movement problem, I noticed that the function to interact with the chest would not work.
The “OnCollisionEnter2D” function is only called one time, that means it will only check if the key is pressed at the first moment of the collision. I guess you want that the chest can be opened/closed the whole time the player is over the chest.

OnCollisionStay2D would work, but maybe the best way is to give the chest a script which handels those functions.

If you are creating an grid-based game, I would not work with collisions. Just remember where the crate is by using a 2D map array.

PS: I’m very new to Unity, feel free to correct me if I’m wrong or if there is a better way to handle it :wink:

You’re not wrong; read what I posted above. :wink:

–Eric

OnCollisionStay worked wonderfully, I also changed the movement to a more basic omni-directional style. Thanks for the help!

are change to something like below your movement is to great.

pos += Vector3.left * speed * time.deltatime;

The other problem you may have is that your player may push the enemies out of the way if that is the case post back here your PM me I’ll help with this part, your dungeon crawlers is similar to my game.

some times OnCollisionStay will not fire use both OnCollisionStay and OnCollisionEnter as well.

further more movement is better in FixedUpdate not Update ONLY do the calculations in Update.

physics can be used, it’s depends on the game I use it just fine