Moving 2d Object - 2d trigger ignored?

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:

void Start()
{
   MyObject.canMoveLeft = true;
   MyObject.canMoveRight = true;
}

void OnTriggerEnter2D ( Collider2Dcol )
{
   if (objectSide == Side.left)
   {
      MyObject.canMoveLeft = false;
   }
//same as above for right side
}

void OnTriggerExit2D ( Collider2Dcol )
{
   if (objectSide == Side.left)
   {
      MyObject.canMoveLeft = true;
   }

//same as above for right side
}

The “MyObject” script (on the game object) contains this for the movement of the object:

if (canMoveRight)
{
   if (gesture.swipe == EasyTouch.SwipeType.Right)
   {
      //Move the object right
   }
//same as above for left side
}

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.

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

Any Unity Admins who are able to assist in this? @hippocoder do you know?

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.

1 Like

Wow, had no idea about this! Thanks I’ll check it out!

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)

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

publicstaticintw = 10;
publicstaticinth = 20;
publicstaticTransform[,] grid = newTransform[w, h];

…but never worked with a grid before.

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?

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.

1 Like