Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Top Down Colliders Issue

Discussion in '2D' started by Krass2509, Sep 25, 2016.

  1. Krass2509

    Krass2509

    Joined:
    Sep 25, 2016
    Posts:
    4
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public Sprite playerNorth;
    7.     public Sprite playerEast;
    8.     public Sprite playerSouth;
    9.     public Sprite playerWest;
    10.     public GameObject chest;
    11.     public Sprite chestOpen;
    12.  
    13.     private SpriteRenderer rend;
    14.     private float speed = 1.0f;
    15.     private Vector3 pos;
    16.     private Transform tr;
    17.  
    18.     void Start ()
    19.     {
    20.         pos = transform.position;
    21.         tr = transform;
    22.  
    23.         rend = GetComponent<SpriteRenderer>();
    24.  
    25.         if(rend.sprite == null)
    26.         {
    27.             rend.sprite = playerNorth;
    28.         }
    29.     }
    30.  
    31.     void Update ()
    32.     {
    33.         OnMove();
    34.     }
    35.  
    36.     void OnMove()
    37.     {
    38.         if (Input.GetKeyDown(KeyCode.A))
    39.         {
    40.             rend.sprite = playerWest;
    41.  
    42.             pos += Vector3.left;
    43.         }
    44.  
    45.         if (Input.GetKeyDown(KeyCode.D))
    46.         {
    47.             rend.sprite = playerEast;
    48.  
    49.             pos += Vector3.right;
    50.         }
    51.  
    52.         if (Input.GetKeyDown(KeyCode.W))
    53.         {
    54.             rend.sprite = playerNorth;
    55.  
    56.             pos += Vector3.up;
    57.         }
    58.  
    59.         if (Input.GetKeyDown(KeyCode.S))
    60.         {
    61.             rend.sprite = playerSouth;
    62.  
    63.             pos += Vector3.down;
    64.         }
    65.  
    66.         transform.position = Vector3.MoveTowards(transform.position, pos, speed);
    67.     }
    68.  
    69.     void OnCollisionEnter2D (Collision2D other)
    70.     {
    71.         if (other.gameObject.tag == "Chest")
    72.         {
    73.             if (Input.GetKeyDown(KeyCode.E))
    74.             {
    75.                 SpriteRenderer cOpen = chest.GetComponent<SpriteRenderer>();
    76.                 cOpen.sprite = chestOpen;
    77.             }
    78.         }
    79.     }
    80. }
    81.  
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    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.
     
  3. Krass2509

    Krass2509

    Joined:
    Sep 25, 2016
    Posts:
    4
    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).
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Grid-based games should not use physics, but use an array to represent the world and base everything off that.

    --Eric
     
  5. NowKnown

    NowKnown

    Joined:
    Jul 23, 2015
    Posts:
    5
    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 ;)
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're not wrong; read what I posted above. ;)

    --Eric
     
  7. Krass2509

    Krass2509

    Joined:
    Sep 25, 2016
    Posts:
    4
    OnCollisionStay worked wonderfully, I also changed the movement to a more basic omni-directional style. Thanks for the help!
     
  8. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    are change to something like below your movement is to great.

    Code (CSharp):
    1. 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