- Home /
How to get center of collider in hierarchy
Hi, I want to find the Y position of 2 colliders in the following Hierarchy:
Player
--Colliders
---Collider1
---Collider2
So I have in a script attached to Player:
var c1:GameObject; var c2:GameObject; c1= GameObject.Find("Player/Colliders/Collider1"); c2= GameObject.Find("Player/Colliders/Collider2");
function Start () { Debug.Log ("C1: "+c1.transform.position.y); Debug.Log ("C2: "+c2.transform.position.y); }
Both of them are returning 5, what Do I need to get the correct centers?
Values are .3 and -.6
Thanks in advance.
Answer by Ashkan_gc · May 17, 2011 at 05:54 PM
you are getting the transform's position which is the position of the gameobject itself that can be different from the center of your collider.
you can get a reference to object's collider by writing:
c1.collider
but Collider class don't have position/dimention information of your collider so you need to know the type of your collider and get it using GetComponent, let's say it's a SphereCollider
c1.GetComponent(SphereCollider).center
is the center of the sphereCollider attached to c1. it's a value in object's local space (0.3) as you said and you should add transform.position to it if you want to have it in world space.
Or use c1.collider.bounds.center -- this will work for any type of collider.
Answer by Eric5h5 · May 17, 2011 at 05:48 PM
Are you looking for transform.localPosition? transform.position is world space.
Answer by Xav_m · May 17, 2011 at 10:14 PM
Thank you very much, used:
c1.GetComponent(SphereCollider).center
and got what I needed. Regards.
Your answer
Follow this Question
Related Questions
Move sprite depending on its collider position 1 Answer
How would i find the center of an object with a Y axis offset to find the center on the top face? 1 Answer
How to Access GameObject.collider.cente in #pragma strict 1 Answer
FPS collider center offsets camera pivot? 0 Answers
How to get the center of a collider face and the halfExtents to the corners of that face? 0 Answers