- Home /
Move Collider2D programatically
well, someone made this question in 2013 http://answers.unity3d.com/questions/579823/move-collider2d-position.html and it has no answer. I couldn't find yet something to point me if is there a way to do this but what I've tryed until now was:
collider2D.bounds.center.x = 0.1
or
collider2D.bounds.size.x = 0.1
however it has no effect on my scene..so could someone explain to me if I'm doing something wrong or if there is a way to move my Collider2D?
thanks
Answer by robertbu · Aug 20, 2014 at 02:46 PM
I don't think there is a generic way to move a Collider2D. You are going to have to do a GetComponent on the specific type of Collider2D you have on the object, and then manipulate that collider based on the properties exposed. For example if you have a box or circle collider, you can just set its center, but for a polygon collider, you would have to change the local space coordiantes of the polygon.
so when I use "collider2D" without "c" capital that means Im getting all colliders Im using on the object that my script was fixed?
No, you are getting the base class for the specific Collider2D on that object. That is all the specific colliders like circle, box, and polygon are derived from a base class. You could just cast the result, but 1) this does not save you the GetComponent() call since Unity does it under the hood, and 2) it is safer to use GetComponent(). You won't want to be casting a circle collider to a box collider.
Your answer
Follow this Question
Related Questions
collider2D.bounds.Contains not working properly 3 Answers
Getting Collider2D.bounds.extents to update immediately (2D) 0 Answers
How to calculate a Collider2D's bounds 0 Answers
CircleCollider2D bounds.center, bounds.extents and radius severely innacurate 0 Answers
Can you find the length or “bounds” of an edge collider2D using Bounds? 0 Answers