Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ricksname · Jun 17, 2013 at 07:19 PM · animationjavascriptenemydistance

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by roojerry · Jun 17, 2013 at 07:25 PM

     if (Distance < attackRange)
     {
        attack ();
     }
     else if (Distance < chaseRange)
     {
        run ();   
     }

should do it

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ricksname · Jun 17, 2013 at 07:30 PM 0
Share

Thanks, but afraid not, he's still just running at me!

avatar image
0

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 ();   
     }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ricksname · Jun 17, 2013 at 07:49 PM 0
Share

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!

avatar image Griffo · Jun 17, 2013 at 08:14 PM 0
Share

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 ();   
     }
avatar image ricksname · Jun 17, 2013 at 08:14 PM 0
Share

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.

avatar image ricksname · Jun 17, 2013 at 08:20 PM 0
Share

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!

avatar image ricksname · Jun 17, 2013 at 08:35 PM 0
Share

I could zip the thing up and send it your way if that would make things easier and it isn't too much trouble?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges