var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var hunt : boolean = false;
var myTransform : Transform; //current transform data of this enemy
var distance : int;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
can somebody tell me how i can make the Raycast work in a way, that my hunter only hunts me if nothing is between us???
function Update () {
dist = (transform.position - target.position).magnitude;
if(dist < distance){
hunt = true;
}
else{
hunt = false;
}
var inSight = true;
if(Physics.Raycast(transform.position,inSight, 5)){
inSight = false;
}
if(hunt == true && inSight == true){
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
You have to understand how the function works. It returns true if anything is intersecting the line that's cast, including the player character. What you need to do is add a second clause that can distinguish between player and non-player objects. You do that using the raycast hit properties.
e.g.
var hit : RaycastHit;
var direction : Vector3;
direction = target.position - transform.position;
if (Physics.Raycast (transform.position, direction, hit)) {
if(hit.collider.CompareTag("Player")){
// Nothing between the us and the player object
// Chase the player
}else{
// Something blocking line of sight
// Do something else
}
// Show in the editor the ray
Debug.DrawLine (transform.position, hit.point);
}
Read the documents carefully about which parameters the various versions use:
function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
EDIT:
use debug draw line to show exactly where the ray is being cast
alright... so this is cind of working, but I have a big prob, even if there is clearly a Object in the way of my hunter the var InSight is till true....
why is it not false even if a Object is in the way??
here is the Raycast loop like it is right now:
var hit : RaycastHit;
var direction : Vector3;
direction =target.position - transform.position;
if(Physics.Raycast(transform.position , direction, hit)) {
if(hit == GameObject.FindWithTag("Player")) {
var InSight = hit.distance;