Specific Objects

S

Soren11112

Guest
I am doing a project with raycasting, and I need to find a unique id of the object hit by a collision line is that possible? And, if so how?
 

Bingdom

Googledom
Yes.
You need to store it as a variable, then you can reference from it.

instance = collision_line()
 
M

Maximus

Guest
collision_line will return the first instance that it finds on the line but not the first in the direction of the line (maybe, by chance). If you want to get the closest instance that is colliding with that line you will need to check each instance individually and their distance.

Code:
/// collision_line_first(x1, y1, x2, y2, obj, prec, notme)
// returns closest instance colliding with a line or noone. (closest by their origin position to (x1,y1))
var x1, y1, x2, y2, obj, prec, notme, inst, inst_dist, ans, ans_dist;
x1 = argument0;
y1 = argument1;
x2 = argument2;
y2 = argument3;
obj = argument4;
prec = argument5;
notme = argument6;
ans = noone;
for (var i = 0; i < instance_number(obj); i++)
{
    inst = collision_line(x1,y1,x2,y2, instance_find(obj, i), prec, notme);
    if (inst != noone)
    {
        if (ans != noone)
        {
            inst_dist = point_distance(x1, y1, ans.x, ans.y);
            if (inst_dist < ans_dist)
            {
                ans = inst;
                ans_dist = inst_dist;
            }
        }
        else
        {
            ans = inst;
            ans_dist = point_distance(x1, y1, ans.x, ans.y);
        }
    }
}
return ans;
 
Last edited by a moderator:
Top