Hey all, I’ve been working on an AI Goalie Script for a 3D soccer game. I have been using booleans as a way to check certain parts, which may not be the most practical, but it is getting results that I prefer. I have used Debug.Log to keep checking that whatever I add will be read properly. Right now the AI Goalie goes between 2 points and then will go after the ball when it is in range. When the Goalie collides with the ball, he no longer chases it (which I am going to create something more advances than that after), and returns to navigating between the 2 waypoints. Everything works until the Goalie collides with the ball, and returns to the waypoints. He will return to the first one, but won’t navigate back to the second one. Any help would be appreciated!
private void NavPoints()
{
if (waypoints.Length == 0)
{
return;
}
AIPlayer.destination = waypoints[destPoint].position;
destPoint = (destPoint + 1) % waypoints.Length;
}
private void FollowBall()
{
Vector3 dirToPlayer = transform.position - Ball.transform.position;
Vector3 newPos = transform.position - dirToPlayer;
AIPlayer.SetDestination(newPos);
}
private void OnCollisionEnter(Collision coll)
{
if(coll.gameObject.name == "Ball")
{
hasCollided = true;
follow = false;
destPoint = 0;
Debug.Log("AI Stopped Chasing Ball");
Ball.transform.position = AIBallSpawn.transform.position;
NavPoints();
}
}
// Start is called before the first frame update
void Start()
{
hasCollided = false;
follow = false;
AIPlayer = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(transform.position, Ball.transform.position);
if (!AIPlayer.pathPending && AIPlayer.remainingDistance < 0.5f && distance > followDist && follow == false && hasCollided == false)
{
NavPoints();
}
if (distance < followDist && hasCollided == false)
{
FollowBall();
}
}
}