I am attempting to code an AI system for a game I am making and I wanted to do so without using waypoints or navmesh (purely dynamic). However when I try to code a statement for detecting how close the AI is to a wall or object I get the statement Operator ‘<’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘int’. How can I get this to work?
if ((hit.transform.tag == "Obstacle") && (hit.point - transform.position) < 2) {
do something
}
Two ways:
(hit.point - transform.position).magnitude < 2.0
Or:
Vector3.Distance(hit.point - transform.position) < 2.0
According to robertbu answer,if you want exactly compre distance with an int number you must typecat it. like this snippet:
.
.//your variable declaration
public int desiredistance=2;
private float disttoobj;
//put this line of code where you want compare distance
disttoobj=Vector3.Distance(hit.point , transform.position);
if((int)disttoobj < desiredistance)
{
//do something here
}