• 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
Question by unity_tKUZtCIEUdxI3g · Dec 29, 2020 at 01:13 AM · unity 2dbugs

Ant has to reach the goal but sometimes will spaz after reaching it. How do I fix this?

In the game there is an ant that wanders randomly around the map by picking a point and then going to that point(this repeats after the ant has reached the point). The objective is to guide the ant to a goal on the map. However, sometimes after the ant has reached the goal, it will start spazzing(rotating in small increments very quickly) instead of picking a new position. I've tried debugging numerous times but can't find a cause for this. Help would be appreciated! Here is the code: private void Update() { Vector3 lookDir = randomPoint - (Vector2)transform.position; float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90; print(angle); print(randomPoint); Vector3 rotation = new Vector3(0, 0, angle); rb.rotation = Mathf.Lerp(rb.rotation, angle, Time.deltaTime * 20); if (Mathf.Abs(rb.rotation-angle) < .1) transform.position = Vector3.MoveTowards(transform.position, randomPoint, Time.deltaTime * speed); else transform.position = savePos; } private void FixedUpdate() { if (Vector3.Distance(transform.position, randomPoint) <= 1) { savePos = transform.position; print("yes"); ChoosePos(); } for (int i = 0; i < goals.Count; i++) { if (Vector3.Distance(transform.position, goals[i].transform.position) <= 7) { savePos = transform.position; randomPoint = goals[i].transform.position; return; } } for (int x = 0; x < GameObject.Find("GameManager").GetComponent<GameManager>().coinList.Count; x++) { if (Vector3.Distance(transform.position, GameObject.Find("GameManager").GetComponent<GameManager>().coinList[x].transform.position) <= 7&& GameObject.Find("GameManager").GetComponent<GameManager>().coinList[x].gameObject.activeSelf) { savePos = transform.position; randomPoint = GameObject.Find("GameManager").GetComponent<GameManager>().coinList[x].transform.position; return; } } Vector3 lookDir = randomPoint - (Vector2)transform.position; lookDir.Normalize(); lookDir *= 4; GetComponent<PolygonCollider2D>().enabled = false; RaycastHit2D hit = Physics2D.Linecast(transform.position, transform.position + lookDir); GetComponent<PolygonCollider2D>().enabled = true; Debug.DrawLine(transform.position, transform.position + (transform.up*4), Color.red); if (hit.collider != null) { savePos = transform.position; ChoosePos(); } } void ChoosePos() { float xPos = Random.Range((Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0)).x), Camera.main.ViewportToWorldPoint(new Vector3(.95f, 0, 0)).x); float yPos = Random.Range((Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0)).y), Camera.main.ViewportToWorldPoint(new Vector3(0, .85f, 0)).y); randomPoint = new Vector3(xPos, yPos); } private void OnTriggerEnter2D(Collider2D collision) { if(collision.tag == "Goal") { collision.gameObject.SetActive(false); collision.gameObject.GetComponent<Goal>().MovePosition(); ChoosePos(); print("yes"); for(int i = 0; i < goals.Count; i++) { if (goals[i].gameObject.activeSelf) return; } GameObject.Find("GameManager").GetComponent<GameManager>().gameOver = true; }else if(collision.tag == "Coin") { collision.gameObject.GetComponent<Coin>().Collect(); GameObject.Find("GameManager").GetComponent<GameManager>().UpdateCoins(); } }

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

115 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

Related Questions

How do I fix this? I dont understand anything. 2 Answers

My game freezes when i try and load level 1? 1 Answer

Some functions do not work when I build the game.,Some functions don't work when I build the game 0 Answers

Unity application goes dark 0 Answers

Unity2D Vector based movement 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