- Home /
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(); } }