I’ve been working on this Proximity AI script for a few weeks now, I have been tweaking and improving by asking for help and finally the script has no compiler errors and works without any issues. However the AI object I assigned the script to, doesn’t move forward towards the ball but instead moves away from it as the ball gets near it. What can I do to fix this?
public class Opponent : MonoBehaviour {
public GameObject Ball;
public float range;
public float speed;
Rigidbody rb;
void Start()
{
rb=GetComponent<Rigidbody>();
}
void Update()
{
range = Vector3.Distance(Ball.transform.position, transform.position);
if (range < 40)
{
transform.LookAt(Ball.transform.position);
}
if (range < 30 && range > 15)
{
rb.AddForce(Vector3.forward*25*speed);
}
}
}
This is the code I’ve been working on, any suggestions on how to fix the problem will be great. Thanks