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

Moving 2d Object - 2d trigger ignored?

Discussion in 'Physics' started by CoolWater, Jan 2, 2015.

  1. CoolWater

    CoolWater

    Joined:
    Jan 2, 2015
    Posts:
    2
    Hi

    I have moving objects in my scene that the player can swipe left or right.

    These 2D objects have two 2D trigger colliders - one on the left side of the object, the other on the right.
    If two objects are next to each other, these triggers activate, and with script, stop the player from moving the object on top of each other.

    For instance, if I have one object on the left, then a new object moves into a position to the right of it, I'm not able to swipe this new object to the left (thereby stopping the object from moving on top of the other).

    This is the script I'm using to do this (this appears on the object trigger:

    Code (csharp):
    1.  
    2.  
    3. void Start()
    4. {
    5.    MyObject.canMoveLeft = true;
    6.    MyObject.canMoveRight = true;
    7. }
    8.  
    9. void OnTriggerEnter2D ( Collider2Dcol )
    10. {
    11.    if (objectSide == Side.left)
    12.    {
    13.       MyObject.canMoveLeft = false;
    14.    }
    15. //same as above for right side
    16. }
    17.  
    18. void OnTriggerExit2D ( Collider2Dcol )
    19. {
    20.    if (objectSide == Side.left)
    21.    {
    22.       MyObject.canMoveLeft = true;
    23.    }
    24.  
    25. //same as above for right side
    26. }
    27.  
    The "MyObject" script (on the game object) contains this for the movement of the object:

    Code (csharp):
    1.  
    2. if (canMoveRight)
    3. {
    4.    if (gesture.swipe == EasyTouch.SwipeType.Right)
    5.    {
    6.       //Move the object right
    7.    }
    8. //same as above for left side
    9. }
    10.  

    Now the issue is that if you swipe over and over on an object vigorously, it can jump onto the cell next to it, as though eventually you can force the triggers to ignore each other/move through each other. The triggers are a decent size on each size (not, say, a pixel), and I'm not sure how to stop this from happening as it can be a game breaker.

    Any suggestions?

    Thanks.
     
  2. CoolWater

    CoolWater

    Joined:
    Jan 2, 2015
    Posts:
    2
    No one can help or make any suggestions on this? Been trying various solutions, such as increasing the size of the collider (but that opens other issues without resolving the current problem).

    Thanks all
     
  3. Clearwater

    Clearwater

    Joined:
    Jan 7, 2015
    Posts:
    16
    Any Unity Admins who are able to assist in this? @hippocoder do you know?
     
  4. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    178
    I would suggest that instead of relying on OnTriggerEnter to determine if you can swipe left or right, you instead just check with Physics2D.OverlapArea to determine if you can or can't swipe that direction.

    You can do this when you detect a swipe or if you need to always know then just check on FixedUpdate. It shouldn't be too expensive.
     
    Clearwater likes this.
  5. Clearwater

    Clearwater

    Joined:
    Jan 7, 2015
    Posts:
    16
    Wow, had no idea about this! Thanks I'll check it out!
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    Are they moving in a grid steps or smoothly/freely?

    if grid, then could check if next cell is empty from array or using raycast (if objects are aligned on rows)
     
  7. Clearwater

    Clearwater

    Joined:
    Jan 7, 2015
    Posts:
    16
    I'd like them to move freely using physics on the vertical, but horizontally moved by defined steps (2d).

    I have been starting to look at something like
    Code (csharp):
    1.  
    2. publicstaticintw = 10;
    3. publicstaticinth = 20;
    4. publicstaticTransform[,] grid = newTransform[w, h];
    5.  
    ..but never worked with a grid before.
     
  8. skusku

    skusku

    Joined:
    Jan 4, 2015
    Posts:
    17
    How many blocks are there that can be filled ? Wouldn't it be the easiest solution to just store 0s and 1s in a Vector3[] (posX, posY, isFilled) for every possible spot and check the desired velocity ( i assume you use non kinematic rigidbodys if youre moving freely ) of the object, with some vector math against the position it wants to go before applying the velocity?
     
  9. Clearwater

    Clearwater

    Joined:
    Jan 7, 2015
    Posts:
    16
    We're actually changing things up a bit and using kinematic for controlled movement as we want tighter control, but still have the option of turning it off and using physics during other parts of the game.

    Using a vector3[] is a good idea. Was thinking how to get around to storing values. I was considering arrays etc, but wasn't sure how to apply the values for position + on/off... so this is great thanks.
     
    skusku likes this.