Can I create kinematic collision objects of dynamic length?

So I have a laser that I built off of this example, my plan for it was to shoot it out by scaling the x value of the laser’s game object, then use the position variable of the kinematic collision object to change the scale of the laser to whatever it connects with.
However, while the laser’s go scales just fine, its hitbox remains its own little gray rectangle from the start:
image

the only information on it I’ve seen (that I understand, at least) is about using the “allow dynamic transformations” option, which I have turned on but it doesn’t seem to effect anything. Is there something simple that I might be missing- the collision type or some set scale/size message that exists for collision objects? or am I going to have to just bite the bullet and use a different approach?

This would be better handled by using a raycast.

See here.

To get the point where the laser is pointing, you can do something like this:

local start_point = go.get_world_position() -- this is assuming the laser starts from the centre of the game object holding the script. Change as needed to get the correct position.
local angle = go.get(".", "euler.z") -- again, this gets the rotation of the game object this script is attached to.
local max_range = 5000 -- max distance the laser can go. If there is no collision, the raycast will return nil
local end_point = start_point + vmath.rotate(vmath.quat_rotation_z(math.rad(angle)), vmath.vector3(0, max_range , 0)) -- Putting it all together to get a point some distance in the direction the laser is facing

ray = physics.raycast(start_point, end_point, {hash("ground")}) -- change to whatever collision you need

local laser_scale = max_range -- this is set up as a fallback for when the laser fails to hit anything within the max range
if ray then
	scale = vmath.length(start_point - ray.position)
end
go.set("laser_go", "scale.x", scale) -- should work as long as the laser sprute is 1 pixel wide and shifted by -0.5 within its game object.

I modified an implementation of a laser from my current project, so it should work, but let me know if there are any issues.

1 Like

is the scan_range anything specific? it doesn’t seem to be included in the code block.

Ah, sorry. Leftover from the original version of the script. It was meant to be the max_range variable. I edited the post.

This may be a silly question, but where would this be placed or implemented?
I’m currently using a game object for the weapon, which has a factory.create for the laser object. The laser has a script and on creation forms a parent-child relation with the weapon then uses the update function to change the scale.
would it work with this approach or does this require different format?

It should work fine from a script attached to the laser itself. Just change the last line to refer to it and you’re good to go:

go.set(".", "scale.x", scale)

Controlling the laser from the parent script would also work, but you’d need to make sure all references are to the laser go, not the go of the parent script.