I am trying to create a script to have an enemies head rotate to the players position with a damping effect so the enemy head isn’t facing the player every second…It also detects if the player is in range of the enemy …This is what i have so far…
var lookAtTarget : Transform;
var damp = 6.0;
var range = 100;
var leftRay : GameObject;
var rightRay : GameObject;
var mode = "idle";
function Update ()
{
//rotation becomes slower when following
if(Vector3.Distance(transform.position, lookAtTarget.position) < range)
{
//Difference between two points in 3D space
var rotate = Quaternion.LookRotation(lookAtTarget.position - transform.position);
//set the rotation to become slower
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
Debug.Log("got you");
}
if (Vector3.Distance(transform.position, lookAtTarget.position)>range){
Debug.Log("dont")
transform.LookAt(null);
}
}
The above script rotates the head when the enemy is in range… But now i am trying to detect when the enemies head stops rotating and is looking straight at the player. This is because i want the enemy to shoot light rays out of it’s eyes when the player stops moving and the enemy head is pointed directly at the player.
Also, how can i have objects interfere with looking at the player. For example, if the player is behind a wall, the enemy cannot see the player. Please, all help is greatly appreciated!!!
I got my first problem fixed with the help of that code…
I was experimenting with physic.raycast earlier and this is the code i was trying to get to work:
var dist = Vector3.Distance(lookAtTarget.position , transform.position);
var hit : RaycastHit;
if(Physics.Raycast(transform.position, dist, hit , range))
{
Debug.Log("I don't see you");
}
else
{
Debug.Log("i got you");
}
i was trying to simulate a player hiding behind an object which causes the robot to stop the rotation of it’s head and player is then not in the enemies view…
but i really couldn’t get the this to work and was unaware as to where to put this in my current code…
Again, here is the code i am working with
var lookAtTarget : Transform;
var damp = 6.0;
var range = 100;
var leftRay : GameObject;
var rightRay : GameObject;
var mode = "idle";
function Update ()
{
//rotation becomes slower when following
if(Vector3.Distance(transform.position, lookAtTarget.position) < range)
{
//Difference between two points in 3D space
var rotate = Quaternion.LookRotation(lookAtTarget.position - transform.position);
//set the rotation to become slower
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
}
//look at nothing if not in range
if (Vector3.Distance(transform.position, lookAtTarget.position)>range)
{
transform.LookAt(null);
}
//Find out when the enemy is looking at the player
if ( Vector3.Dot(transform.forward.normalized, (lookAtTarget.position - transform.position).normalized) > .99)
{
Debug.Log("got ya");
}
}
Object in the way detection can be done like this :-
if(Physics.Linecast(transform.position, target.position, hit))
{
if(hit.collider.gameObject.tag != "Player")
{
Disengage();
return false;
}
else
{
Attack();
return true;
}
}
right, psyclones way is better. BUT FYI your problem (or one of them at least) with Raycast was that dist is a scalar (single value) ,not a Vector3. A danger of untyped unityscript!