Controlling animation frames with Distance?

Hi guys and girls, for the last few hours I’ve been trying how to figure out how to control a animation with distance but of course no joy. What I’ve been trying to was use animation[“Animation_Name”].normalizedTime = 0.5F; with a script reading the distance between two cubes. But I just cant figure it out.

Now what I would really like is something like this: If Distance between “Redcube” and “Greencube” = 0, Animation = Startingpoint (0 Frames). Then when the Redcube moves towards the green cube its plays the animation frame by frame. So not If Redcube Distance < 9.0F than it plays the whole animation. So then when the Redcube is really close to the green one Distance between cubes = 0.9, and animation is at frame 0.9 (well something like that)

It would be a grand help :slight_smile: Thank you, James

These were the scripts what I was using:

public class Pullback_Distance : MonoBehaviour {

public float Pullback = 0.0F;

	void Update() {
	animation["Pullback_hand"].normalizedTime = Distance.thedistance;


public class Distance : MonoBehaviour {

public Transform other;
static public float thedistance = 0.0F;
// Update is called once per frame
void Update () {

if (other != null){ 
        float dist = Vector3.Distance(other.position, transform.position);
            print("Distance to other: " + dist);


Though Nothing happends, HELP and THANKS!

In the Distance script you are not setting theDistance variable, you are setting the dist variable, which will go out of scope immedately. Just change it so theDistance variable is equal to the Vector3.Distance calculation.

You probably also need to define some sort of maximum distance because you are likely not going to only move the Cubes within a distance of exactly 1 unit of each other?