How to make bodyVelocity move in the direction of player's mouse?

Hello! I want to make a shooting game where I can fire a fireball at a certain direction using bodyVelocity. But it only goes forward. How to I make bodyVelocity go in the direction of the player’s mouse?

here’s my code

local bodyVel = Instance.new("BodyVelocity")
bodyVel.MaxForce = Vector3.new(5000,5000,5000)
bodyVel.Velocity = (HumanoidRootPart.CFrame.lookVector*100)
bodyVel.Parent = fireball

The reply should include on how to make the fireball move in the direction of the players mouse. Thanks!

4 Likes

A players mouse has a .Hit property which is a cframe of the position of the mouse. you can plug this in with lookVector

A player’s mouse position is only accessible from the client, yet I assume you’re shooting the fireball from the server.

You’ll want to include the player’s mouse position (or the desired shoot direction) as an argument to the remote that you’re firing to tell the server to shoot the fireball. The local player’s mouse position can be obtained by game.Players.LocalPlayer:GetMouse().Hit.Position.

Thanks guys. But I’m not sure how do I apply the mouse’s hit position. Can you tell me where to put the mouse position?

The equation for your fireball’s velocity should look something like this:

bodyVel.Velocity = (mousePos - HumanoidRootPart.Position).Unit * 100
9 Likes

I tried and I got :

Unit is not a valid member of CFrame

What dhouls I replace “Unit” with?

mouse.Hit returns a cframe, to get its position you should put mouse.Hit.Position

1 Like

Oh wait. I replaced Unit with lookVector and it worked. Thanks guys!

2 Likes

Sorry to notify everyone when the solution has already been found but I have this code,

throwHandleClone.BodyVelocity.Velocity = (mousePosition - humanoidRootPart.Position).lookVector * 100

and it gives me the error of: “lookVector is not a valid member of Vector3”.
Do any of you know what I’m doing wrong here (once again I apologise for notifying everyone)?

1 Like

Instead of .LookVector put .Unit

3 Likes