# Unity3d : Animator canceling script

I create a sway script for my flashlight and attached it to the gameobject(flashlight).

Now the problem is that when I enable the animator in the gameobject(flashlight) the sway doesnt work,but when I disable it,it does work. This is the script :

#pragma strict

var flashLight : Transform;
var xSwayAmount : float = 0.1;
var ySwayAmount : float = 0.05;
var maxXAmount : float = 0.35;
var maxYAmount : float = 0.2;
private var vector3 : Vector3;
var smooth : float = 3.0;

function Start () {
vector3 = flashLight.localPosition;
}

function Update () {
var fx : float = -Input.GetAxis("Mouse X") * xSwayAmount;
var fy : float = -Input.GetAxis("Mouse Y") * ySwayAmount;

if(fx > maxXAmount)
fx = maxXAmount;

if(fx< -maxXAmount)
fx = maxXAmount;

if(fy > maxYAmount)
fy = maxYAmount;

if(fy < -maxYAmount)
fy = -maxYAmount;

var detection : Vector3 = new Vector3(vector3.x + fx, vector3.y + fy, vector3.z);
flashLight.localPosition = Vector3.Lerp(flashLight.localPosition, detection, Time.deltaTime * smooth);
}


Has anyone got any idea why is this happening ? I need the animator to play my animations so I can't just disable it.Thank you.

I assume the flashlight object is a child of the model with the Animator component, otherwise it should not be affected.

The solution is to make all of the scripted changes in LateUpdate() instead of Update(). This works because the Mecanim system runs in the Update() loop and since you want to overwrite the bone position, you just have to run right after it, ie. LateUpdate().

You can see my question, Mixing Unity Mecanim and Scripted Bones/States, I made a while ago over at Unity Answers. It has a long list of other topics dealing with your exact same problem.

If you want to see a successful example in the real world, here is a script that I have written before that mixes scripted and Mecanim animation. The script moves the head bone around according to your mouse input (think FPS camera).

Small boilerplate:

## C#:

public class MixingScriptedBonesWithMecanim : MonoBehaviour
{
public Transform flashLight;

void LateUpdate()
{
// Do all bone changes here
// ex.
// flashLight.transform.position = new Vector3(Mathf.PingPong(Time.time, 1f), flashLight.transform.position.y, flashLight.transform.position.z)
}
}


## JavaScript:

#pragma strict

var flashLight : Transform;

function LateUpdate()
{
// Do all bone changes here
// ex.
// flashLight.transform.position = new Vector3(flashLight.transform.position.x + Mathf.PingPong(Time.time, 1f), flashLight.transform.position.y, flashLight.transform.position.z)
}

• I swapped the Update() with the LateUpdate() and it sort of works.It do have the sway effect but it looks like super laggy,like really really laggy.Maybe im asking too much but got any idea how to make it run smooth ? The script without the animator is pretty smooth so that's not the problem,I think. – Ssiro Jul 17 '14 at 12:28
• @Ssiro Press the stats button in the Game view, is the FPS a reasonable number? If your game is laggy(<20), then there is a performance problem with one of your scripts. What is your smooth value say inside the editor on the component (is it still 3?)? – MLM Jul 17 '14 at 14:51
• Yes,it is still 3.My game is not laggy at all,only the flashlight sway LOOKS laggy.It's like teleporting in short distances. – Ssiro Jul 17 '14 at 17:28
• @Ssiro I just played around with you flashlight script and it is definitely the flashlight script and nothing to do with the animator, etc. If you draw a gizmo for the detection point you will notice that is jumpy as well. This is because of the way you calculate it. Your script is simply flawed and needs work. The original question asked was solved by LateUpdate() and if you still need help on this separate issue I suggest asking another question. – MLM Jul 17 '14 at 18:35
• If it has nothing to do with the animator then how come it works without it ? – Ssiro Jul 17 '14 at 18:52