P
P
Pavel2018-09-07 21:11:10
Unity
Pavel, 2018-09-07 21:11:10

How to make sure that objects do not overlap each other when dragging?

There are 2 identical GameObjects on the stage, on each of them hangs a collider2D for the size of the sprite (not a trigger), rigidbody2D. They also have 2 of my components: Rectangle and DragableComponent.
Each of the squares can be dragged after the cursor - DragableComponent is responsible for this (implemented using OnBeginDrag, OnDrag). One of the conditions: objects should not overlap each other when dragging. However, if you drag one object through another too quickly, it flies through the collider of another object and, accordingly, through the object itself.
Unlike a bullet in shooters, there is no particular point in doing a raycast (the trajectory of the mouse movement may change), increasing the collider is also not an option, because. one more object may refuse nearby and then the dragging will stop immediately (on onCollisionEnter I break the Drag event).
Tell me what other options are there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2018-09-07
@youkerni

If I understood correctly, then you want collider physics to work on dragged objects?
then almost certainly you don’t just translate or change transform.position
and you need to move the physical body with physics)
first try all this on fixedUpdate (take out the physics cycle)
but in general I would do that during the drag - the object "stretches" / follows behind the target)
(and of course still not through moving to Update, but synchronized with physics)
again, it will not save you from the option that you moved from one corner to another. calculates the physics step at which it "does not notice" other colliders.) jumps over)
there is another option to hang the Rigitbody quite straight, and give it strength. and everything is done completely according to physics) but it already depends on the game design and in general on the desire to do this)
well, otherwise you have a very abstract description, so here are the abstract options for you)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question