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 Carbongrip · Jan 01, 2014 at 07:35 PM · c#rotationaitimeturning

Script is Flipping Out!

Hey everyone in the script I tried making my ai ship turn while a variable is greater than zero but it never stops and spins way faster that if it was not in this while statement. Can someone take a look at how this part of the script is rotating the ship and show me how to make it rotate for a set time like I want. Please don't just throw documentation at me I already looked at it… thats how I came up with this is all that documentation.

void Avoid()

     {
         if(dist < 20)
         {
             followTarget = false;
             StartCoroutine (turn());
         }
     }
 
     IEnumerator turn()
     {
         if(followTarget == false)
         {
             //wait the cooldown time in a loop
             float turningTime = 5f;
             while (turningTime > 0)
             {
                 transform.Rotate(0, turnSpeed * Time.deltaTime, 0 * Time.deltaTime, Space.World); //and then turn the plane
                 //decrement turningTime
                 turningTime -= Time.deltaTime;
                 //let unity free until next frame
                 yield return null;
             }
         }
     }
Comment
Add comment · Show 1
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 Carbongrip · Jan 01, 2014 at 11:46 PM 0
Share

Well those are kind of ok but this is really driving me crazy now… I am trying to make a ship ai that the enemy follows me if distance is something which I have done. But then I need it to circle around me if the distance gets to a certain point that way it can't run into me… then I need to make it think about in some way running from me under a certain circumstance. This is a 3d space style game...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sath · Jan 01, 2014 at 09:03 PM

//set the variable

 float turningTime;

make the Avoid like this

 void Avoid(){
     if((dist < 20)&&(followTarget)){
        followTarget = false;
        turningTime = 5f;
     }
 }



and finally put this in Update

 void Update(){
         
         turningTime -= Time.deltaTime;
         
         if((!followTarget)&&(turningTime>0f)){
             transform.Rotate(0, turnSpeed * Time.deltaTime, 0 * Time.deltaTime, Space.World);
         }
         
     }
Comment
Add comment · 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
0

Answer by tanoshimi · Jan 01, 2014 at 08:26 PM

Are you calling Avoid() from your Update() loop by any chance? If so, it's probably calling turn() in every frame in which dist is less than 20, and you've stacked multiple coroutines to rotate the object, each of which last for 5 seconds.

Try adding a Boolean variable to indicate whether turn() is already running (set it to true/false at each end of the coroutine), and only call it from Avoid() if not already running.

Comment
Add comment · 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

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

18 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 avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Unity AI Obstacle Avoidance Help 3 Answers

How to work out dogfight rotations for AI 0 Answers

How can I turn the direction of bicycle in Unity? 3 Answers

NavMeshAgent resume original position and facing direction C# 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