Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 CycloneWhale · Apr 30, 2020 at 05:36 PM · while-loop

While loop crashes unity

Whenever my change direction function is called unity crashes. I'm pretty sure it has to do with the while loop but I don't know why. I'm trying to make the enemy follows the player, but right now whenever the player changes direction the enemy changes direction instantly. I want to make it so that the enemy gradually changes direction.

 public class EnemyAI : MonoBehaviour
 {
     private Animator anim;
     private LimbController limbs;
     private GameObject player;
 
     public float speed;
 
     private int playerDir;
     private int lastPlayerDir;
     private float dir;
 
 
     private void Awake()
     {
         limbs = GetComponent<LimbController>();
         player = GameObject.FindGameObjectWithTag("Player");
         anim = GetComponent<Animator>();
     }
 
 
     private void Start()
     {
         playerDir = player.transform.position.x > transform.position.x ? 1 : -1;
         lastPlayerDir = playerDir;
         dir = playerDir;
     }
 
 
     private void Update()
     {
         playerDir = player.transform.position.x > transform.position.x ? 1 : -1;
         if(lastPlayerDir != playerDir)
         {
             ChangeDirection(playerDir);
         }
 
         if(player.transform.position.x - transform.position.x > 5 || player.transform.position.x - transform.position.x < -5)
         {
             limbs.body.bone.velocity = new Vector2(dir * speed, limbs.body.bone.velocity.y);
             anim.SetBool("isWalking", true);
         }
         else
             anim.SetBool("isWalking", false);
     }
 
 
     private void ChangeDirection(int changeDir)
     {
         while(dir != playerDir)
         {
             dir += changeDir * Time.deltaTime;
         }
         lastPlayerDir = playerDir;
     }
 }




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
0

Answer by yogummben · Apr 30, 2020 at 05:41 PM

If your while loop is endless then unity will crush. Happened to me before. You should check is there a way to end your while loop.

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 CycloneWhale · Apr 30, 2020 at 06:14 PM 0
Share

Ok thanks i figured it out :)

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

125 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 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 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 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 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 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 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

[Closed]Unity crash, while/for loop 3 Answers

Can I use a while() in the function OnDrawGizmos() ? 1 Answer

Rotate object as long as it's colliding 0 Answers

XmlTextReader problem 1 Answer

Coroutine "While" Setup 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