Animation conflicting with scripted rotation

I’ve found this same problem in several other threads and answers but none of the solutions have worked for me.

The basic problem: I have a soldier character (pic 1) which, as far as I know, is properly rotated and rigged (rigging courtesy of Mixamo). My objective is, when it’s sphere collider-trigger encounters an enemy, it plays an animation and rotates so that the barrel of the rifle is pointed at said enemy.

When testing the animation, the rifle is pointed in the same direction as the base model’s z axis (pic 2), so rotating the base model to face the target should work, right?

But no. In Play mode, Animations work fine, but its orienting based on the z-axis of the root, not the model (pic 3).

function LateUpdate () {
    if (enemy) {
        var localTarget: Vector3 = transform.InverseTransformDirection(enemy.transform.position - transform.position);
        var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
        var adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
        transform.Rotate(Vector3.up, adjustedAngle);
    }
}

function OnTriggerEnter (other : Collider) {
    if (other.gameObject.tag == "EnemyInfantry") {
        advance = false;
        target = true;
        unit.SetBool("Advance", advance);
        unit.SetBool ("Enemy", target);
    }
}

I’ve tried this with LateUpdate, FixedUpdate, altering the animation curves, checking and unchecking Apply Root Motion, checking and unchecking Write Defaults on the animations themselves… I can’t think of what else to try. Any thoughts?

Pic1:
2365349--160591--Image1.png

Pic2:
2365349--160592--Image2.png

Pic3:
2365349--160593--Image3.png

If you are changing the root transform position/rotation you should do it either in OnAnimatorMove or if you are using statemachine behaviour in OnStateMove. You have to this this way otherwise the animator will not know that you did change the root and the internal root position won’t be updated correctly

1 Like

Not sure I fully understand this.

Tried putting this:

function OnAnimatorMove () {
    if (enemy) {
        var localTarget: Vector3 = transform.InverseTransformDirection(enemy.transform.position - transform.position);
        var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
        var adjustedAngle = Mathf.Lerp(0, targetAngle, Time.deltaTime * SwivelSpeed);
        transform.Rotate(Vector3.up, adjustedAngle);
    }
}

instead of the LateUpdate I had previously and seems like nothing changed.