Hello, I am developing a side scrolling shooter game with Unity5 2D. However I’m having trouble programming the enemy to check if it can see the player in order to start chasing him (the enemy should be able to see the player if there is no wall/block-object between them).
So how can I check if there is no obstacle between two objects?
My apologies if this is a beginner’s question, I’m still new to Unity. Thanks in advance.
if (Physics.Linecast (transform.position, target.transform.position)) {
//there is something in the way
}
If it’s a simple see/not-see case, why not use Physics.Linecast.
Physics.Linecast Doc
If you want to see if, lets say the first object you hit is the player or something else, try:
RaycastHit hit;
if (Physics.Linecast (transform.position, target.transform.position, out hit)) {
if(hit.transform.tag == "player"){
//do something
}
}