So I have been working on an AI Player that will not only follow a ball to block a shot, but I want to make it so when he collides with the ball, he no longer chases the ball. I have a pretty keen idea on where to go after that to get what I want him to do, but this middle part has been frustrating. I’ve tried instantiating and getting rid of the ball, but that didn’t seem to work. As of right now, he is just keep going after the ball, even with boolean values. Here’s a snippet:
private void OnCollisionEnter(Collision stopScore)
{
if (stopScore.gameObject.tag == "ball" && followBall == true)
{
Ball.transform.position = ballSpawn.transform.position;
Ball.GetComponent<Rigidbody>().useGravity = false;
followBall = false;
}
if(followBall == false)
{
GotoNextPoint();
return;
}
}
void Update()
{
float distance = Vector3.Distance(transform.position, Ball.transform.position);
if (distance < followDist)
{
Vector3 dirToBall = transform.position - Ball.transform.position;
Vector3 newPos = transform.position - dirToBall;
agent.SetDestination(newPos);
}
else if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
2 things.
You need use Debug.Log to track issues.
Also please read
If you see someone who needs to know about code tags, please link them to this post:
Please use code tags when posting code.
You can "tag" your code by typing [984347--130826--Screenshot 2015-03-16 10.33.58.png] around your code.
There is no overt "Code" tag button that automatically tags a selection as code.
That being said, however, we can Insert code directly into the post using the "Insert Code" button:
[image]
This will bring up an "Insert" window were we can paste our code and c…
Thanks for that tip! The thing is I’m not finding “issues” as to why the AI continues to go after the ball after it is collided with
So I made some adjustments, and getting closer to what I want. Except now, when he collides with the ball, he does not return to walking between his waypoints that are set up.
void GotoNextPoint()
{
if (points.Length == 0)
return;
agent.destination = points[destPoint].position;
destPoint = (destPoint + 1) % points.Length;
}
private void OnCollisionEnter(Collision stopScore)
{
if (stopScore.gameObject.tag == "ball" && followBall == true)
{
Ball.transform.position = ballSpawn.transform.position;
Ball.GetComponent<Rigidbody>().useGravity = false;
followBall = false;
}
}
void Update()
{
float distance = Vector3.Distance(transform.position, Ball.transform.position);
if (distance < followDist)
{
Vector3 dirToBall = transform.position - Ball.transform.position;
Vector3 newPos = transform.position - dirToBall;
agent.SetDestination(newPos);
}
else if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
if(followBall == false)
{
Debug.Log("Turning Gravity Back On");
Ball.GetComponent<Rigidbody>().useGravity = true;
GotoNextPoint();
return;
}
}
}
Put more Debug.Log where necessary.
Monitor to what is happening and if is happening what you expect, or not.