Can I change the position of the gameObject from the script DURING an animation?

I tried to Lerp the position of the gameObject using script, during my animation (using Unity’s animation feature)

At the end of the animation, I want the gameObject to move to a dynamic target, but it doesn’t move. The script locates where the target is, then Lerps the gameObject to it. I used an animation event to call the script.

I even added a blank frame to the animation, 10 seconds after the end of the animation, but that didn’t help.


Any suggestion?

My script is the usual Lerp code:

var startPosition = gameObject.transform.position;
			var endPosition = chosenTarget.transform.position;
			var timer = 0.0;
			var time = 3;

			while (timer < time) {
				timer += Time.deltaTime;
				gameObject.transform.position.x = Mathf.Lerp(startPosition.x, endPosition.x, timer/time);
				gameObject.transform.position.y = Mathf.Lerp(startPosition.y, endPosition.y, timer/time);

				yield ;

You can use animation events for that. Just above the keyframes you can right click and add an animation event. it looks like a little white tick. This will allow you to call functions on any script that is on the same object as your animation.

Place one at the end of your animation that runs the code to move to the dynamic target.

Found a solution. You have to temporarily disable the animator for the Lerp and re-enable it afterwards