So the box collider moves but the mesh render doesn't move. What am I doing wrong? Code (CSharp): using UnityEngine; using System.Collections; public class MoveDoors : MonoBehaviour { public float rotateXAmount=0f; public float rotateYAmount=0f; public float rotateZAmount=0f; bool rotated=false; GameObject me=null; void Start(){ me=this.gameObject; } public void DoorMove(){ if(rotated==false){ me.transform.Rotate(rotateXAmount,rotateYAmount,rotateZAmount); rotated=true; } else{ rotated=false; me.transform.Rotate(-rotateXAmount,-rotateYAmount,-rotateZAmount); } } }
Now it works but i'm running into a new problem because it doesn't have a correct pivot. It's a middled pivot even if I add a parent object to it. It doesn't work at all then.
There are numerous ways to do this. The way I do door is to position the door properly in the 3d tool such that the hinges are at the origin. For example: The pivot point is at the hinges. The the door can be rotated using: Code (csharp): door.transform.eulerAngles=new Vector3( door.transform.eulerAngles.x, someNewYAngle, door.transform.eulerAngles.z); All it does it rotates it around the Y-axis (Unity-up) axis. If it wasn't centered, you could probably rotate it around a specific point, but you would need to know the world point which might not be easy to determine. You could put a Unity cube at the hinge and then tell the door to rotate around the position of the dummy cube. Code (csharp): door.transform.RotateAround(dummyCube.transform.position, Vector3.up, speed * Time.deltaTime);