- Home /
[RESOLVED]Raycast problem
I have an enemy that goes between wapoints and if the player is in a certain area then chase. However i want to do a raycast from the enemy to the player so that if the player is standing behind a wall but still in the chase distance then it wont chase the player.
It seems very simple but for some reason the raycast is getting the better of me. I want it to change a boolean to false if the enemy can see it but there isn't a wall between.
here's the snippet of my script i am working on. I have defined the player as a transform with tag Player The draw line works.
function Update () {
var hit : RaycastHit;
Debug.DrawLine(transform.position, player.position, Color.red);
if(Physics.Raycast(transform.position, player.transform.position, hit)) {
if(hit.collider.gameObject.name == "player") {
touchingWall = false;
Debug.Log("touching wall false");
} else {
touchingWall = true;
Debug.Log("touching wall true");
}
}
}
Answer by aldonaletto · Apr 07, 2012 at 02:14 AM
@rutter is right: usually Linecast is the best choice in this case - but you must check if the object hit by the line is the target you're looking for: if it's not, you have something in between.
var target2 : Transform;
function Update () { var hit: RaycastHit; if (Physics.Linecast(transform.position, target2.position, hit)){ // if line hits something... if (hit.transform == target2){ // and it's the target... print("I can see you!"); } else { print("Something hides the target"); } } }
Thank the lord. Sorry that was such a lengthy process guys. Before i posted this question I had been fiddling with raycasts for about 2 or 3 hours and it all just turned into scribble.
Thanks a lot!
$$anonymous$$ake sure to accept the answer that most helped you by clicking the tick. :)
Answer by rutter · Apr 06, 2012 at 09:58 PM
Raycast()
doesn't want two positions; it wants one position and a direction (for example, by subtracting your target's position from your own, you can get a vector pointing from yourself to your target).
You might be thinking of `Physics.Linecast()`, which does take two positions.
So what, i would do something along the lines of:
var playerDirection : Vector3 = (player.postion - position.transform);
then
Physics.Raycast (playerDirection, hit, playerDirection.magnitude);
Sorry i have been looking at this all day so really struggling.
Ok, i will look at the script reference for linecast first. Then try the above. Will amend this question if it's fixed. In the meantime if anyone else has an idea let me know please.
Ok, I have looked into Linecast and seems like a much more sensible way to do it. I have tried it the way that the script reference suggests and i still get problems. I then learnt that there is issues sometimes with the cast detecting the object it is casting from, so i set they layer on my enemy to ignore casts. But i'm still having problems, they detection seems to be messed up and i can't quite figure why. Despite various debug logs.
I have looked at some answers with the similar problem and i have come up with this:
var target2 : Transform;
function Update () {
var hit : RaycastHit;
Physics.Linecast (transform.position, target2.position, hit);
if(hit.transform.tag == "Player"){
print("See");
touchingWall = false;
Debug.Log("not touching anything");
}
}
however for SO$$anonymous$$E reason, despite the fact this has worked for someone else I am getting told that the line "if(hit.transform.tag == "Player") {" is not set to an instance of an object
You star! Thank you so much, that fixed my problem. For reference:
if(Physics.Raycast(transform.position, (inco$$anonymous$$gDestination - transform.position) , out hit, $$anonymous$$athf.Infinity))
Works...
if(Physics.Raycast(transform.position, inco$$anonymous$$gDestination, out hit, $$anonymous$$athf.Infinity))
Doesn't...
Your answer
![](https://koobas.hobune.stream/wayback/20220613070015im_/https://answers.unity.com/themes/thub/images/avi.jpg)