Unity Rotation

From Schmid.wiki
Jump to: navigation, search

Rotation Relative to Prefab Rotation

A recurring problem when working with artists is that the models are rotated wrongly. To fix this, we either tell the artists to fix the models, or - more conveniently, rotate the prefab. However, when rotating in the code, this prefab rotation becomes a problem.

The following example shows how to rotate relative to the original prefab rotation:

public Thing : MonoBehaviour {

    Quaternion originalRotation;
    Vector3 initialFacing;

    public void Start() {
	
        // store original rotation quaternion
        originalRotation = transform.rotation;

        // In what direction does the prefab face?
	initialFacing = transform.InverseTransformPoint(Vector3.forward);
    }


    public void Update() {

	Vector3 facing = computeNewFacing();

	// compute new rotation
	Quaternion newRotation = 
		originalRotation *
                Quaternion.FromToRotation(initialFacing, facing);

	transform.rotation = newRotation;
    }
}
Personal tools