Reputation: 2503
My goal is to rotate the engine of a plane.
My plane have 2 engines and is represent like this in my hierarchy :
If I analyze my plane from the inspector, here's what I get in Center/Global mode :
And here's the Pivot/Global mode :
If I try to rotate my engine, here's the result :
As you can see, my engine rotate not around his pivot axis but around his center axis. How can I make it rotate around his pivot axis ?
The code I call to make it rotate :
foreach(Transform child in planeId_Object_Dictionnary[newPlane.Flight].transform){
if (child.name == "Engine"){
child.Rotate(new Vector3(0, 30, 0) * Time.deltaTime*100, Space.Self);
}
}
Upvotes: 0
Views: 1710
Reputation: 125245
The Pivot point of the 3D model is not centered. Since you have 3DSMax, it is better to center the pivot point from a 3D software so that you don't need to create new dummy GameOject to be used as the center point. Watch this video to see how to center the pivot point. Just select the Engine and click the Center to Object button. You should so the-same to all the other parts of the plane. Save and re-import it to Unity again.
Your code
if (child.name == "Engine")
{
child.Rotate(new Vector3(0, 30, 0) * Time.deltaTime*100, Space.Self);
}
is not efficient. Don't compare GameObject by name. Compare it by instance
, instance id
or by tag
. In your case, tag
is appropriate.
Create a new tag in the Editor and name it Engine. Select all the Engine model parts then change their tags
to Engine. Now, you can use the code below which is more efficient and does not allocate memory.
if (child.CompareTag("Engine"))
{
child.Rotate(new Vector3(0, 30, 0) * Time.deltaTime * 100, Space.Self);
}
Upvotes: 4
Reputation: 411
You create a empty gameobject and place it at where you want to rotate. The thing you want to rotate, make child of that empty gameobject. Now rotate that game object.
transform.Rotate(new Vector3(0, 30, 0) * Time.deltaTime*100, Space.Self);
This will make object rotate.
Upvotes: 1