Hi guys, I have a little problem here. Basically I have a guard patrolling a set path using waypoints (the waypoints are stored within an array and are named “1” and “2”. There are only two waypoints. The guard patrols the area as he should so this is fine. When the player gets too close to him, he chases them away until the distance between the player and the waypoint is too great. This is where the problem occurs.
The issue:
When I try and implement the lookat function for the integer I receive this error message:
Assets/FBX Files/Scripts/AI.js(48,57): BCE0023: No appropriate version of ‘UnityEngine.Transform.LookAt’ for the argument list ‘(UnityEngine.Transform)’ was found.
When the guard returns to the waypoint because the player is too far away, he continues to look at the player when he moves back as I cannot use the transform.LookAt function because of the variables state (an integer / array). Is there anyway I could get the guard to look at his current waypoint when he is returning from chasing the player? I only need the API I am not asking for code, I am capable haha
Here’s the script so far:
var bible : GameObject;
var speed : float = 40;
var waypoint : Transform[];
var currentWaypoint : int;
var loop : boolean = true;
var player : Transform;
var distance : int;
var test : boolean = false;
var waypoint1 : Transform;
var waypoint2 : Transform;
function Update()
{
if (currentWaypoint == 1)
{
transform.LookAt(waypoint2);
}
if (currentWaypoint == 0)
{
transform.LookAt(waypoint1);
}
if(currentWaypoint < waypoint.length){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var distFromPlayer : Vector3 = player.position - transform.position;
var velocity = rigidbody.velocity;
if (moveDirection.magnitude <1)
{
currentWaypoint++;
}
if( distFromPlayer.magnitude < distance){
velocity = Vector3.zero;
transform.LookAt(player);
target = player.position;
velocity = (player.position - transform.position).normalized * speed;
noiseLevels.noise = 100;
noiseLevels.spotted = true;
velocity = (player.position - transform.position).normalized * speed;
if ((player.position - waypoint[currentWaypoint].position).magnitude > 200)
{
Debug.Log("Going back");
transform.LookAt(waypoint);
noiseLevels.spotted = false;
target = waypoint[currentWaypoint].position;
velocity = moveDirection.normalized * speed;
}
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
if (noiseLevels.noise > 20)
{
distance = 60;
}
if (noiseLevels.noise > 40)
{
distance = 90;
}
if (noiseLevels.noise > 60)
{
distance = 120;
}
if (noiseLevels.noise > 80)
{
distance = 150;
}
if (noiseLevels.noise > 99)
{
distance = 180;
}
}