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 stingman · Apr 23, 2012 at 04:24 AM · animationraycasttransformcoroutinerange

Enemy Animation to not stop when player runs out of range

When my player runs within range of an enemy the enemy performs his attack animation. However if I run out of range while the animation is being performed he stops doing the animation. I want him to play through the entire animation instead of stopping like he currently does. I'm sure it's something simple but I'm at a loss after trying several things... here is my code if anyone has any suggestions:

     #pragma strict
 var target : Transform;
 var pirate: GameObject;
 var detectRange : float = 30;
 var rotateSpeed : float = 10;
 private var inRange = false;
 
 function Start () {
 pirate.animation.Play("PirateIdle");
 pirate.animation["PirateStrike"].wrapMode = WrapMode.Once;
 pirate.animation["PirateIdle"].wrapMode = WrapMode.Loop;
 }
 
 function Update () {
 
 var hit : RaycastHit;
         var tgtDirection = target.position - transform.position;
         var tgtDistance = tgtDirection.magnitude; // get distance to the target
         if(Physics.Raycast(transform.position,tgtDirection , hit)) {
            if (tgtDistance <= detectRange){
            inRange = true;}
             if (inRange) {
             PirateStrike();
             }
             if (!inRange) {
             pirate.animation.Play("PirateIdle");
             }
           }  
           }
             
 
 function PirateStrike() {
              var tgtDirection = target.position - transform.position;
             tgtDirection.y = 0;
             pirate.animation.Play("PirateStrike");
             yield WaitForSeconds(1);
            
             var rotate = Quaternion.LookRotation(tgtDirection);
             transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * rotateSpeed);
             }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Apr 23, 2012 at 05:12 AM

  if (tgtDistance <= detectRange) { // If in the range then attack, 
                                    //no need of a bool here
    PirateStrike();
  }
  else if (tgtDistance >= detectRange && !animation.IsPlaying("PirateStrike") ) {
     pirate.animation.Play("PirateIdle");
  }

    

You can try something like this. The if inside checks if you are out of range and if the animation is not playing meaning that if playing the idle animation is not launched. Also, I put an else if since if you are out of range or in range there is no need for checking the other one.

Comment
Add comment · Show 3 · 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 stingman · Apr 23, 2012 at 05:39 AM 0
Share

Thanks for the suggestion. I tried this at one point as well. It doesn't work though... the attack animation just keeps playing in a loop no matter what range the player is at.

avatar image fafase · Apr 23, 2012 at 05:57 AM 0
Share

I can't find where you are setting inRange back to false to stop the attack. I edit my post without inRange. Then ins$$anonymous$$d of having 2 ifs, first checking distance and flipping inRange, you now check the distance and start the anim.

avatar image stingman · Apr 23, 2012 at 05:49 PM 0
Share

thanks got it working. appreciate the help!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

function behaves unexpectedly inside a coroutine. 2 Answers

Possible to transform and objects position smoothly with confined movement dictated by raycasting, but controlled by user? 0 Answers

How to get info of object within certain range?(Javascript) 1 Answer

The Weirdest Bug Ever: GameObject Transform Appears in Editor to Be in One Spot but Instantiates and Registers in Another 2 Answers

Error in Raycast Detect Ground for Falling Animation 1 Answer


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