- Home /
How to check if two objects collide without a collider
hey, so i have a project where something when collides with a line renderer it dies but this line renderer is always moving/updating so i cant have an collider on it. So i want to know if there is a way to check it without a collider
Hi @PedroA$$anonymous$$ - I guess then you'll have to use pure math. You know your object's position, start and end points of line in 3d space. $$anonymous$$aybe look for solutions for line intersection between some shape like sphere or plane - whatever is needed.
Why not attach a collider to the line renderer? You can move/update the collider too.
Answer by Ermiq · Aug 15, 2018 at 10:15 PM
I can see two (tree actually) ways to solve this: 1. attach some collider to the line gameobject. 2. which is I prefer, use Physics.Raycast
or Physics.Linecast
. Ray is more suitable when you know the direction but don't know what a length the line will be. so you set the direction and max distance. The Line option is if your line has fixed length.
Vector3 origin;
Vector3 direction; // or Vector3 endPosition; and then get direction with direction = endPosition - origin
float maxLineLength;
int layerOfKillableObjects // or you can use Physics.AllLLayers so ray will trigger when intersect with any objects
void Update ()
{
RaycastHitInfo hitInfo;
if (Physics.Raycast (origin, direction, out hitInfo, maxLineLength, layerOfKillableObjects, QueryTriggerInteraction.Ignore)
{
Transform deadMan = hitInfo.transform;
// kill that man
}
}