Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with Raycasting/BodyVelocity?

Asked by 10 years ago

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

0
This isn't raycasting BlueTaslem 18071 — 10y
0
It includes raycasting, since the endposition is required for the calculation. MightyWanderer 30 — 10y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
0
Thanks, it worked really well. This is a much easier way of calculating. :P MightyWanderer 30 — 10y
Ad

Answer this question