Moving Custom Models based on MousePosition

I’ve been trying many different ways to move spaceships in my game using the mouse. I tried Velocity of the Core Part of the model (which tore the ship apart, literally). Then I tried BodyVelocity, which kept moving upwards no matter where my mouse was.

I am totally lost.

What is the best way for me to move a model based on Speed (W/S) and where your mouse is?

Any help is appreciated!

1 Like

Are you trying to rotate or move the ship based off of the mouse?

In the case of you wanting to move the ship, this is how I would do it:

For my ships, I would first make sure I weld all of the parts of the ship to a center part or the PrimaryPart. To move it, I would create a BodyVelocity parented to the PrimaryPart.
Now, to find the offset of the mouse, you would have to do something like this:

local xOffset = (Camera.ViewportSize.X/2 - Mouse.X) / 1000
local yOffset =  (Camera.ViewportSize.Y/2 - Mouse.Y) / 1000

These offsets will give you the position of where the mouse is in the space. Now to actually move it, I’m not entirely sure what you would have to do, but just test around with camera vectors until you get a desirable answer.

In the case of you wanting to rotate the ship, this is how I would do it:

Same thing, but instead, I would use a BodyAngularVelocity. Using the formula I gave you from above, I would set the AngularVelocity to this on the event of the mouse moving:

BodyAngularVelocity.AngularVelocity = (Camera.CFrame.upVector * xOffset) + (Camera.CFrame.rightVector * yOffset)

If you have any questions, ask me. Hopefully I helped you. :slight_smile:

1 Like

You can use BodyGyro and BodyVelocity to be able to move your spaceship. Heres how:
BodyVelocity’s Velocity property uses world space, not local space, which is why it always moves up. You can solve this by always making the velocity the lookvector of the spaceship’s primary part. Make sure the MaxForce property is high enough.

You can also use BodyGyro in conjunction with BodyVelocity to fix your problem. You need to set the BodyGyro’s CFrame property to the looking direction from the primary part of the ship to the mouse. You can also fix this by using a CFrame and putting the primary part’s position and the mouse position. The BodyGyro should now be looking at your mouse.

Here is an example:

--[[Vel is the BodyVelocity
Gyro is the BodyGyro
Primary is the ship's primarypart]]--
function updatevelocityandgyro(speed,lookpos)
Gyro.CFrame = CFrame.new(Primary.Position,lookpos)
Vel.Velocity = Primary.CFrame.lookVector*speed
end

By using the function updatevelocityandgyro with the selected speed and the position of the mouse, you should be able to allow it to move around while looking at the mouse.
Alternatively, if you wanted it to always move precisely to the mouse, you could use this:

function updatevelocityandgyro(speed,lookpos)
Gyro.CFrame = CFrame.new(Primary.Position,lookpos)
Vel.Velocity = CFrame.new(Primary.Position,lookpos).lookVector*speed
end

Good luck!

2 Likes

@OsterDog Thanks for the reply, but still the problem remains.

I have tried your formulas (lookpos being mouse.Hit.p) and yet:
Screenshot_97
My mouse barely effects the ship at all. When I hover the mouse of the ship, it freaks out and twitches. It also keeps spinning.

Thanks for commenting, can you help me with this?

@iiJaguar_Cub Your method made steering look epic, but I found it quite hard to control. Is there anything I could do to make it easier?

Also, what do you mean by:

Thanks for the reply!

You can use the TargetFilter property to be able to make the mouse ignore the ship, which removes the problem of twitching and erratic movement.
You may also have to set the MaxTorque of the BodyGyro to a really high amount, so that the ship actually can rotate.

Still sideways…?
Screenshot_98

To make it easier, simply just change the amount the value gets divided by.

If the sensitivity is too high, divide the value by a higher amount. If it is too low, make the value lower.

local xOffset = (Camera.ViewportSize.X/2 - Mouse.X) / 1000
local yOffset =  (Camera.ViewportSize.Y/2 - Mouse.Y) / 1000

For example, instead of dividing the value by 1000, you can change it to

local xOffset = (Camera.ViewportSize.X/2 - Mouse.X) / 2000
local yOffset =  (Camera.ViewportSize.Y/2 - Mouse.Y) / 2000

Just test around with it. Hope that helps.

When I mean test around with the camera vectors, I meant that you should use the different vectors there are of CFrame of the camera. For more information look at this.

1 Like