Lock Rotation on a Child Object But Allow Parent to Rotate

I attached an object to my player object as a “flag” (literally). I want the “flag” to face the camera at all times while allowing the play object to rotate. I tried to set rotation of the child object (the “flag”) in the Update() function. It worked to a certain extend, but whenever the player object makes a turn, the child object would rotate with the parent object for one frame and then snap back to the right angle. The end result is a constant flickering of the child object.

I couldn’t figure out how to fix this. Any thoughts?

Here’s the code I am using:

var X_RotationLockedAt : int ; //locking the rotation at a certain angle for X
var Y_RotationLockedAt : int ; //locking the rotation at a certain angle for Y
var Z_RotationLockedAt : int ; //locking the rotation at a certain angle for Z


function Update()
{
     transform.rotation = Quaternion.Euler(X_RotationLockedAt, Y_RotationLockedAt, Z_RotationLockedAt);
     
}

Thanks!

Psuedo code:

void Awake()
Vector3 fixedRotation = transform.rotation;

void LATEUPDATE()
transform.rotation = fixedrotation;

Thats it.

A warning message appears that says

BCE0022: Cannot convert ‘UnityEngine.Vector3’ to ‘UnityEngine.Quaternion’.

But changing Update() to LateUpdate() in the original script seems to do the trick. Thanks!! :slight_smile:

Not sure why LateUpdate() would work but Update() wouldn’t though. Wouldn’t I want the rotation to happen earlier instead of later? Any ideas?

1 Like

That’s because it should be Quaternion fixedRotation, not Vector3.