var visionAngle=0.3; // 1 is directly in front 0 is to the side -1 is directly behind
var visionRange = 30.0;
var hearRadius = 10.0;
var thePlayer : Transform;
function findPlayer(){
var hit : RaycastHit;
var distanceToPlayer = Vector3.Distance(transform.position, thePlayer.transform.position);
var forward = transform.TransformDirection(Vector3.forward);
var toPlayer = thePlayer.transform.position - transform.position;
canSeePlayer = false;
if (Vector3.Dot(forward,toPlayer) > visionAngle distanceToPlayer < visionRange){
if(Physics.Linecast(transform.position, thePlayer.transform.position, hit)){
if(hit.collider.name == thePlayer.collider.name){
canSeePlayer = true;
theTarget = thePlayer;
}
}
}
canHearPlayer = false;
if(distanceToPlayer < hearRadius){
canHearPlayer = true;
theTarget = thePlayer;
}
if(!canSeePlayer !canHearPlayer){ //This has to ultimately be the final else
function FindNextWaypoint();
}
}
Please be less obscure when posting. throwing 200 lines of code and asking what is wrong with it is not very convenient, and it doesn’t make people want to help you very much. (think of the role on the other foot, would you want people being so obscure with you?)
On close inspection, I see that you’ve created Skynet. We would be doomed, except that there is an AI SEE problem, so Skynet cannot AI SEE us.
What I mean to say is, you’ve got an AI script that you pulled from someone else, and it doesn’t do what you seem to think it should do. How can we help?
var thePlayer : Transform;
function Update(){
if(!thePlayer){ // if we currently don't have a player
thePlayer = FindPlayer(); // go find a player. This expects that the function FindPlayer will return a Transform or null... (Very important)
}
}
function FindPlayer(){
if(GameObject.Find("Player")){ // if we can find the player
return GameObject.Find("Player").transform; // remember, we have to return a transform, not a game object.
}
return null; // we HAVE to return something or we will get an error.
}