Box Collider isn't rotating with Game Object

Posted by pek on Game Development See other posts from Game Development or by pek
Published on 2013-07-03T17:13:13Z Indexed on 2013/07/03 17:19 UTC
Read the original article Hit count: 391

Filed under:
|

I have a method that creates a room by instantiating a prefab, places it in a grid and the re-sizes the collider based on a room definition (location in grid, rotation, width and height).

Here is the method:

public void CreateRoom(RoomAction action)
{
    GameObject roomGameObject = Instantiate(this.roomPrefab, Vector3.zero, action.RoomPrefab.transform.rotation) as GameObject;
    roomGameObject.transform.parent = this.transform;
    roomGameObject.transform.localPosition = new Vector3(action.MansionOffsetX, 0, -action.MansionOffsetY) * this.blockSize;
    roomGameObject.transform.localPosition += new Vector3((action.Room.Width * this.blockSize) / 2, 0, -((action.Room.Height * this.blockSize) / 2));

    BoxCollider roomCollider = roomGameObject.GetComponent<BoxCollider>();
    roomCollider.isTrigger = true;
    roomCollider.center = new Vector3(0, this.height / 2, 0);
    roomCollider.size = new Vector3(action.Room.Width * this.blockSize, this.height, action.Room.Height * this.blockSize);

    roomGameObject.transform.RotateAroundLocal(roomGameObject.transform.up, Mathf.Deg2Rad * -90 * action.Rotation);
}

The problem I'm having is that, while the room rotates correctly, but for some reason, the collider isn't rotating with the game object.

Here is a screenshot:
Room collider problem

Any idea on what am I doing wrong?

© Game Development or respective owner

Related posts about unity

Related posts about rotation