- Home /
Raycast problems in my AI
hey, I try to make this AI working for quite a while now.. The problem i have with it is, that a the raycast always returns positiv, even if the Object it hits is not my player!! I dont know what I am doing wrong! this is my whole AI... I know that it is not the best jet ;P
var target : Transform; var moveSpeed = 3; var rotationSpeed = 3; var hunt : boolean = false; var myTransform : Transform; var distance : int; var inSight : boolean = false; var runSpeed = 9; function Awake() { myTransform = transform; }
function Start() { target = GameObject.FindWithTag("Player").transform;
}
function Update () {
dist = (transform.position - target.position).magnitude;
if(dist < distance){
hunt = true;
}
else{
hunt = false;
}
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;
}
}
if(InSight <= distance){
inSight =true;
}
else{
inSight = false;
}
if(hunt == true){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
if(inSight == true){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
if (Player_Simply.running == true) {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * runSpeed * Time.deltaTime;
}
}
Answer by skovacs1 · Oct 19, 2010 at 03:56 PM
Physics.Raycast will raycast against every object within the layers specified by the cullingMask which you leave as kDefaultRaycastLayers, not just against just your player as your question states that you are expecting.
hit
is aRaycastHit
, not aGameObject
.hit == GameObject.FindWithTag("Player")
will always returnfalse
. You should probably usehit.collider.gameObject == GameObject.FindWithTag("Player")
which will actually work.- You declare the InSight variable within the scope of the two if statements and then try to access it in a different scope. This will not work.
- Why do you even bother with
GameObject.FindWithTag("Player")
within your raycast if when you store thetransform
of thisGameObject
intarget
? - You check a boolean condition to set hunt, but you only use it in one place. Why not just check the condition itself? Isn't it the same with inSight?
- You calculate
target.position - transform.position
up to five times - why? - You do essentially the same thing in all three if statements. Why?
- You're not supposed to pass Slerp/Lerp a deltaTime, but an incremental time value unless you want it to always look some small amount towards the target. Since you are also slerp/lerping from a constantly changing rotation to a potentially constantly changing rotation by some tiny control amount, you will never actually reach the target unless your frame rate should slow significantly (in your case to rotationSpeed frames per second).
This does what it seems you are trying to do and should actually accomplish it (Note that the Slerp here is not framerate dependent anymore):
var target : Transform; var huntRange : float = 20.0f; var rotationSpeed : float = 0.1f; var moveSpeed : float = 3.0f; var runSpeed : float = 5.0f; private var speed : float;
function Start() { target = GameObject.FindWithTag("Player").transform; }
function Update() { var hit : RaycastHit; var direction : Vector3 = target.position - transform.position;
if(direction.magnitude < huntRange
&& (Physics.Raycast(transform.position , direction, hit)
&& hit.collider.gameObject == target.gameObject)) {
//Shorter than writing 2-3 ifs that do the same thing
if(Player_Simply.running) speed = runSpeed;
else speed = moveSpeed;
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction),
rotationSpeed);
transform.position += transform.forward * speed * Time.deltaTime;
}
}
thanks for ur great help, but i tried ur script, and the only thing that my hunter is doing is forward! he is not looking at my player nor is he running after him! is it a problem with the script or is it me??? so basicly the new script is doing less then my old one... but ur it looks a lot better :P (this is not supposed to say, that i don't appreciate ur help)
Yeah. Sorry about that. Forgot the transform. before rotation - I'm a little surprised that wasn't an error actually.
It was missing. See where it says I edited the post 2 hours ago? That's when I fixed it.
ok so now its doing everything that it should, but it is ignoring everything in its way. when i put a box in its way with a collider on it, the hunter is just going through it. maybe u can tell me a way, so i can send u my project by now i think that there is a problem in a other script! thanks
Your answer
Follow this Question
Related Questions
Physics.Raycast 2 Answers
Physics.Raycast AI problem 1 Answer
C# Check Physics.Raycast Once 0 Answers