Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with Raycasting/BodyVelocity?

Basically, I'm making a gun. When you click the mouse, it raycasts to where you click with a Y axis of 3 so it just keeps going (not down or up). I'm using a BodyVelocity to get it to go from the gun forward at a constant speed, but because of the way a BodyVelocity works I have to have all of the shots go the same distance so the speed is maintained throughout, so I have to calculate PAST the endpoint of the ray. My problem is that I can't seem to figure out how to do that. It doesn't always fire to where the mouse is, and it mostly messes up when the mouse is near any object. (I know the ray is working, because if I cut out the BodyVelocity and just place the shot at the endpoint it works perfectly).

Here's how I'm calculating the velocity of the BodyVelocity:

--pos is the endpoint of the ray that is cast.
local v3=(Vector3.new(gunhandle.CFrame.X,3,gunhandle.CFrame.Y)-pos).unit
newshot.BodyVelocity.velocity = -(Vector3.new(0,3,0) + v3*200)

Thanks, --MightyWanderer

2 comments

0

This isn't raycasting

0

It includes raycasting, since the endposition is required for the calculation.

1 answer

✓ Accepted answer

The math you have so far is not at all right.


The direction a handle is pointing is probably:

handle.CFrame.lookVector

Alternatively:

To get the direction to the mouse from the handle:

(mouse.Hit.p - handle.CFrame.p).unit

If you want to flatten either of those to be sure that it isn't moving up / down, you can multiply it component wise:

dir = (dir * Vector3.new(1,0,1)).unit

1 comment

0

Thanks, it worked really well. This is a much easier way of calculating. :P

This discussion is preserved as it was. New answers, comments, and votes are closed.