- Home /
Physics.Raycast
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;
}
}
Which version of Physics.Raycast are you calling? I can't see any match of Physics.Raycast that takes those parameters (http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html)
Answer by spinaljack · Oct 17, 2010 at 04:31 PM
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
Answer by Juri · Oct 18, 2010 at 12:26 PM
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;
Does the object in the way have a collider on it? If not, the ray won't interact with it. If it does: pause the game at a moment where you think the object is in the way, and look closely at the Scene View. You may find that the ray is just missing the object's collider.. Remember also that the ray is directed from the transform of the hunter towards the player's transform. Typically the transform is at the center, but it doesn't have to be. Again, the scene view can help you figure out what's going on.
It has a collider on it but the raycast simply ignores it!!! is there a prob in the code
Your answer
Follow this Question
Related Questions
Physics.Raycast AI problem 1 Answer
Raycast problems in my AI 1 Answer
AI Raycast movement and direction 1 Answer
Physics.Raycast not checking layermask properly? 1 Answer
Collision position - Improving on the 'grounded' mechanic 2 Answers