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

How do I make a projectile fire the way my mouse is aiming?


local a = Instance.new("Part")
	a .Shape = "Ball"
game.Debris:AddItem(a , 5)
	a .Material = "Neon"
	a .CanCollide = true
	a .Size = Vector3.new(3.6,3.6,3.6)
	a .BrickColor = BrickColor.new("Really red")
	a .Transparency = 0
	a .CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(mp,mp,-20)
	
	
	local Bd = Instance.new("BodyVelocity")
	Bd .MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	Bd .Velocity = mp * 100

(This is not the full script as it works if I do not try to make it aim for the mouse position.

local mp = Mouse.Hit.p

This is what it says in the local script that invokes the other scripts.

2 answers

Vectors have two parts: direction and magnitude. Direction is pretty self explanatory. Magnitude is how big the vector is.

The vectors (0, 1, 0) and (0, 5, 0) have the same direction (up), but not the same magnitude (speed of 1 vs speed of 5).

The vectors (0, 1, 0) and (1, 0, 0) have the same magnitude (1) but not the same direction (up, and left).

To get the vector going from point A to point B, we do B - A. If we were to add this to A, we'd get B: (B - A) + A = A

However, this has a magnitude of "the distance from A to B". In other words, if we make our projectile have this speed, it would arrive there instantly.

Therefore, we find something called the "unit vector", which is basically the vector which has the same direction but a magnitude (or speed, in this case) of 1: (B-A).unit

Then, we multiply this unit vector by the speed we want to get a vector going in the correct direction with the correct speed: (B-A).unit * speed.

function getVector(a, b, speed)
    return (b - a).unit * speed
end

I believe this is the formula, if not, try following the person above, yet I think it's not exactly necessary, give this a shot?

a.CFrame = CFrame.new(a.CFrame.p,mp)
Bd.Velocity = a * 100

Hope it works!

13 comments

0

If this works, don't forget to press accept!

0

Ima try both

0

Both did not work.

0

then try putting CFrame.new part at the bottom and forget line 1?

0

I think you messed up with a.CFrame = CFrame.new(a.CFrame.p,) P is position right? I am pretty sure CFrame does not work like that?

0

Nvm I am stupid, lol I replaced my current line for the original setting the CFrame with yours for line 1 instead of making a new thing for line1 above the velocity, lemme try this

0

Alright, if everything works out, don't forget to accept!

0

So here is whats happening. For some reason its firing at insane speeds and is not accurate at all. Like I am facing a certain side of the map and it shoots that way but right

0

So it's firing in the direction of the mouse properly now, but to the right?

0

Something like that, but if I look a diff way I cannot see it at all

0

It's proably firing downwards then

0

Idk try * by the lookVector?

0

Welp none of this is working, I will try to get it some other time, its fine shooting straight from your torso for now. Now I just gotta work on a fly ooF. I am trying to figure out how to make the player face towards the mouse while flying lol. I think it will be more easy to do that though

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