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

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
1
Answered by
fredfishy 833 Moderation Voter
5 years ago
Edited 5 years ago

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
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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!

0
If this works, don't forget to press accept! Ziffixture 6913 — 5y
0
Ima try both happytimeroblox101 36 — 5y
0
Both did not work. happytimeroblox101 36 — 5y
0
then try putting CFrame.new part at the bottom and forget line 1? Ziffixture 6913 — 5y
View all comments (9 more)
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? happytimeroblox101 36 — 5y
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 happytimeroblox101 36 — 5y
0
Alright, if everything works out, don't forget to accept! Ziffixture 6913 — 5y
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 happytimeroblox101 36 — 5y
0
So it's firing in the direction of the mouse properly now, but to the right? Ziffixture 6913 — 5y
0
Something like that, but if I look a diff way I cannot see it at all happytimeroblox101 36 — 5y
0
It's proably firing downwards then Ziffixture 6913 — 5y
0
Idk try * by the lookVector? Ziffixture 6913 — 5y
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 happytimeroblox101 36 — 5y

Answer this question