Detecting whether there's a collider between 2 gameObjects in C#
Hello Unity community.
What's the best way to detect if there's a collider between 2 gameObjects position in a straight line?
Any ideas will be appreciated.
You could try raycasting between the two objects, and returning a collision. I don't really have the time to test this now, but;
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Thank you for answering. Linecast was what I was looking for.
Answer by Veerababu.g · May 26, 2016 at 12:49 PM
line cast will work for you. first hit the middle collider with line cast. after hitting take that hit direction and send a ray in same direction to the another object. try this one
if(Physics.Linecast(transform.position,target.position,out hit))
{
Vector3 raycastDir = hit.point - (Vector3)transform.position;
if(Physics.Raycast(hit.point,raycastDir, out hit1))
{
debug.log("hit");
}
}
Thank you for answering.
I didn't need to do all of that, but thanks to your code I could manage to find out the solution :)
int environment = Layer$$anonymous$$ask.Get$$anonymous$$ask("Environment");
if (!Physics.Linecast(shooter.transform.position, target.transform.position, environment)) {
// Fire!
}
Your answer
Follow this Question
Related Questions
How to draw a collider between two points 0 Answers
Need to move gameObject when triggering collider 0 Answers
It lags when it collides. Help? :( 0 Answers
ui Text not responding to collider 1 Answer
2d Tilemap collidor not working 0 Answers