Hi guys. I can’t figure out this one, so would love to ask you to point me in right direction.
I am trying my hands on simple AI. What i am looking for is methods on how to see if there is target in line of sight and is there a wall in front of him. Do i cast a ray? or how does it work?
For example, 2 ways of doing it:
Looking at all directions at once, and gather info of all objects that are seen. And if one of them is found by tags, take action
Looking only in some degree of direction (line of sight)
I was thinking of raycast, but not sure how it works, what does it actually do. If any of you could just point me in proper direction, i would be very thankful.
No need for code, just tell me what functions do you use, or keywords i can use to google
Debug.DrawRay kind of draw a line out of nowhere. DrawLine did better work, never the less, thanks mate.
Tough i will share code i pasted together from that link from both codes, so it fits my needs, maybe it will help someone else too. Credits only goes to guys in that link - daveyjj and pakfront
How to:
Create 2 or 3 objects, ie Spheres.
Create empty game object.
Assign this script to empty game object.
Drag and drop your balls into empty gameobject’s script part
Press PLAY.
It will spam debug every frame that it saw targetBall. If it hits some other object first, it will say “saw something”. That can be changed using raycast.hit to find out what it hit. Also modify raycast heights so it doesnt ignore target if it hit something really small thats in a way.
To see Debug line press pause. While inside Pause mode, you can move your spheres around for raycast tests.
Enjoy!
RayCastTest.js
var whiteBall : GameObject;
var targetBall : GameObject; // green one
var hit : RaycastHit;
function Update () {
Debug.DrawLine (whiteBall.transform.position,targetBall.transform.position, Color.red);
if (Physics.Linecast(whiteBall.transform.position,targetBall.transform.position, hit)) {
Debug.Log ("i saw something");
if (hit.transform == targetBall.transform) {
Debug.Log ("i saw targetBall");
}
}
else {
//this should ordinarily never happen
Debug.Log ("saw nothing");
}
}