- Home /
How to I vary animations based on proximity?
I've written a script to animate a toon based on his location to me, with an 'idle' animation when he is out of attack range, a 'run' animation when he is within chase range, and an 'attack' animation when he is within attack range. But as soon as I enter chase range, the 'run' animation plays and the 'attack' animation never begins. (The idling animation works fine.) I think it's perhaps because my code would have both run and attack animations happen at once. Not what I want. Is there a way of saying the 'run' animation should play when less than one distance, but also stop when less than a second distance?
My script is attached for reference.
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var Damping = 6.0;
var chaseRange = 15.0;
var attackRange = 1.5;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance > chaseRange)
{
idle ();
}
if (Distance < attackRange)
{
attack ();
}
if (Distance < chaseRange)
{
run ();
}
}
function idle ()
{
animation.CrossFade ("idle");
}
function attack ()
{
animation.CrossFade ("attack");
}
function run ()
{
animation.CrossFade ("run");
}
Thanks a lot.
Answer by roojerry · Jun 17, 2013 at 07:25 PM
if (Distance < attackRange)
{
attack ();
}
else if (Distance < chaseRange)
{
run ();
}
should do it
Answer by Griffo · Jun 17, 2013 at 07:42 PM
try -
var attacking : boolean;
if (Distance < attackRange)
{
attacking = true;
attack ();
}else attacking = false;
if (Distance < chaseRange && !attacking)
{
run ();
}
Thanks Griffo, but this one still isn't working. I tried the second suggestion you made (using if (Distance < chaseRange && Distance > attackRange)) Which acually worked! Then I tried to change the attack range variable, and it stopped working. I changed the variable back, and it still doesn't work! Hugely frustrating!
Are your animations set to loop if so try -
var attacking : boolean;
if (Distance < attackRange)
{
attacking = true;
animation.Stop("run");
attack ();
}else attacking = false;
if (Distance < chaseRange && !attacking)
{
run ();
}
Tried it like this?
function idle ()
{
animation.CrossFade ("idle");
animation["idle"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
}
function attack ()
{
animation.CrossFade ("attack");
animation["attack"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
}
function run ()
{
animation.CrossFade ("run");
animation["run"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
}
Still no luck. The enemy appears to just run at me without ever using the attack animation.
I don't seem to be able to set them to loop. there is no 'loop' tick box next to the animations as I have seen in tutorial videos. When I try the 'boolean' version of the script, I get a tick box appearing in the variables list. I've tried it ticked and unticked, with no success each time. I just can't believe that it would work, and then just stop working without apparent cause. I've triple checked for any mistakes, but I can't find anything!
I could zip the thing up and send it your way if that would make things easier and it isn't too much trouble?
Your answer
![](https://koobas.hobune.stream/wayback/20220613104345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to conect distance with animation? 0 Answers
I can't script enemy animation 0 Answers
enemy shoots with animation 1 Answer
Change animation depending of distance 1 Answer
The problem of Enemy scale when he follow the player (script) . How to fix? 0 Answers