Question by
ranihsn95 · Dec 19, 2016 at 12:50 PM ·
collision2d platformer
How to check if the collision line between two objects is free?
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.
Comment
Answer by TreeFish · Apr 08, 2017 at 02:02 AM
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.
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
}
}
Oh my god I know you posted this like 3 years ago but THANK YOU!
Your answer
