-1
\$\begingroup\$

For starters I'm new to Unity3d. So my question is when I click on an object I want to change its position but without collinding with the other objects and I need a function to check that. I already added a box collider 2d and checked is trigger.

\$\endgroup\$
0
\$\begingroup\$

Unfortunately in Unity there is no way to see if an object is currently colliding. What you need to do is record the last "Good" location & try to move the object. Then override OnCollision and of it is colliding with an object it shouldn't be, move it back to the good location. If you do this you will also want to set a flag (bool moving) saying that you are moving it, and once it is at that location (and hasn't collided) remove that flag.

All that being said, if you are trying to manually move an object & using the collision system you are probably doing it wrong. It would be better to set the velocity of the object such that it move the object in the correct direction & then stop it from moving once it has collided.

More details on what you are doing would also help other craft a better answer.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You can enable and disable the collider on the player to get the desired effect. For example my player is climbing a ladder and I would like him to move through the grounds box collider while he is on the ladder so I create a bool isClimbing and when he connects to the ladder and is climbing is true I would

player.GetComponentInChildren<BoxCollider2D>().enabled = false;

and when he leaves the ladder than re-enable it.

edit- note that if your player does not have a parent then you would use

player.GetComponent<BoxCollider2D>().enabled = false;
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.