- Home /
Rotating a child transform.
When aiming, I dont want my full character to rotate around the X axis, but only the upper-body. Now I've got the upper-body transform, but I cant seem to rotate it.
Could anyone help me figure this out?
void Start()
{
var hips = transform.Find("mixamorig:Hips");
upperBody = hips.Find("mixamorig:Spine");
}
This gets the spine transform through the hips child of the character (I couldn't access it directly, only through hips.)
if (isAiming)
{
var xMouse = Input.GetAxis("Mouse X") * turnSpeed * mouseTurnMultiplier;
upperBody.Rotate(0, xMouse, 0);
var yMouse = -Input.GetAxis("Mouse Y") * turnSpeed * mouseTurnMultiplier;
upperBody.Rotate(yMouse, 0, 0);
}
This does not rotate the upper-body of the character, and I'm stumped as to how. I have no constraints on the parent either.
Would greatly appreciate help with this!
Are you sure that you are putting that snippet in the update? anyway, have you checked that upperBody is what you really want by logs? Try to log y$$anonymous$$ouse and x$$anonymous$$ouse too to see what could be happening.
I did all you suggested trying to figure out my issue, before co$$anonymous$$g here, yes.
Do you have an Animator attached to this GameObject? When a Transform is part of an animation, you can't move it by script.
There is indeed an Animator attached, that makes sense! Thanks for answering my question. Follow-up tho, is there a workaround? Currently looking at unity animations. If anyone has any tips as to where i can pick up some info on this matter, greatly appreciated!
Answer by GoneMadStudios · Oct 22, 2019 at 06:37 PM
As you can see in Unity's script lifecycle, animations are processed after scripts' Update() functions are called:
This means that all changes you make in Update() are overriden by the animator, so you have to do the changes after ProcessAnimation(). You can use LateUpdate() to change Transforms or use OnAnimatorIK(), but this function is only called if the script is in the same GameObject in hierarchy as the Animator.