- Home /
Objects not rotating right half the time based on another object
I'm having issues with rotating objects to face each other based on points within a prefab. The prefabs i have, have multiple entrance/exit nodes in them at different positions. The script i have uses two prefabs currently and pics a node from object 1 to use as an exit and one from object 2 to use as an entrance. The script then rotates and moves object 2 so the nodes are on top of each other and the object is facing the other way. This currently works with two of the nodes in the object but the other two it makes the object face the wrong way, overlapping object 1.
The room has 4 nodes at:
exit 1: 0 degrees - works fine
exit 2: -180 degrees - works fine
exit 3: 270 degrees - Faces wrong way
exit 4: 90 degrees - faces wrong way
The script that handles rotating:
void RotateToMatch()
{
// Get entrace offset local to object rotation
Vector3 entranceOffset = entrance.transform.localEulerAngles;
// Get rotation of exit node
Vector3 exitRot = exit.transform.eulerAngles;
Vector3 objNewRot = (exitRot + new Vector3(0, 180, 0)) + entranceOffset;
Debug.Log("Rotations -- Entraance Offset: " + entranceOffset.ToString() + " -- Exit rotation: " + exitRot.ToString() + " -- Exit Rotation fixed: " + objNewRot.ToString());
// works for room exits 1 and 2
// rotate object to be the inverse of exit node and apply offset
obj2.transform.rotation = Quaternion.Euler(objNewRot);
// works for room exits 3 and 4
//obj2.transform.rotation = Quaternion.Euler((exitRot + entranceOffset);
}
Image examples:
Exit 1 and 2 which work
exit 3 and 4 that dont work:
Your answer
Follow this Question
Related Questions
Reletive rotation problems. (C#) 2 Answers
Rotate object around object with fixed value. 1 Answer
Flip over an object (smooth transition) 3 Answers
transform.Rotate will not work 1 Answer
How do i rotate an object "Once" 1 Answer