- Home /
BoxCollider2D rotates opposite mesh
Hello,
My BoxCollider2D is a child of a gameobject. Within said gameobject is a gun mesh with the collider attached to it. On the left of the player, when I rotate the gun around, the collider lines up perfectly. However, on the right side (when everything is flipped), it rotates in the same direction as it did on the left side, obviously making it not line up with the mesh on the right side. Pictures below:
OK:
Bad:
How can I fix this?
EDIT: More info.
I use a lookat() for the instantiated game object, which again the hierarchy is:
empty GO > gun mesh && collider (collider is on gun mesh, so one child only)
I have to rotate the gun mesh 90 degrees on the y axis, since the lookat() uses the Z coord as the forward facing coord. That reason and the fact that I need a rotating collider are the reasons for using a parent/child relationship in the first place.
Here is my current code for the orientation and hand movement. I am setting the item each update because there is another script that I use to instantiate the weapon or just a fist. Optimization comes later :P (C#):
void Update ()
{
item = gameObject.GetComponent<DrawWeapon>().item.transform; //actively set to whatever item is spawned currently
Vector3 mousePos = camera.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
item.LookAt(mousePos);
if(Vector2.Distance(mousePos, gameObject.transform.position) > 1.5f)
{
item.position = gameObject.transform.position + (mousePos - gameObject.transform.position).normalized * 1.5f;
}
else
{
item.position = mousePos;
}
}
Thanks for looking.
Answer by VTW · Feb 04, 2014 at 01:40 AM
Not a solution, but a workaround. Using a poly-collider2d I am able to get accurate tracking for all necessary rotations.