How do I make a plane fly where my mouse is looking?

Hi, I’m trying to make a Plane. I wrote some code. The only thing my plane can do is turn left and right. It can also accelerate back and forth. (I used Body Velocity for acceleration, and BodyAngularVelocity for rotation). All I have to do is make it fly in the direction my mouse is looking. But how do I do this?? I tried to use many ways, but all it led to was just the bugs and lags. Please, can someone help me implement this? (You don’t have to write all the code on plane, just tell me how to make it fly where my mouse is looking.)

1 Like

As far as i know, you need to get Mouse.Hit.Position and use your head Position and use unit to do that. I never made planes but i think it’d work like this:

direction = (Mouse.Hit.Position - Character.Head.Position).unit
1 Like

I’ll try it, thanks. (you don’t know what exactly I should do with this value? I mean, where do I apply it? in the orientation, or angular velocity?)

1 Like

Maybe both, for movement and rotation

1 Like

You could try to use a BodyGyro instead, feeding it the direction you want it to face (maybe you’ll have to get the angles from the direction vector but it’s not hard). You can get the direction the way Andr_yy said.

Don’t discard BodyAngularVelocity though, as you may want to use it in the future for rolls and stuff

1 Like

Everything seems to be working, but my plane is behaving strangely. how can I fix this? I use 1 line of code to change the rotation of the plane.

local Direction = (Mouse.Hit.Position - Character.Head.Position).unit
-----------
BodyAngularVelocity.AngularVelocity = Direction
3 Likes

Uh oh. Try using BodyGyros for that

how do I get it to rotate using the gyro body? I should use bodygyro.cframe = cframe.new (pos, direction) but what should I use as “pos”?

Not experienced with CFrame, but try putting any positions in there (model’s, head’s etc)

I probably won’t answer soon, since I’ll have to learn body gyro and cframe. But I’ll answer when I can

2 Likes
local Direction = (Mouse.Hit.Position - Character.Head.Position).unit
BodyGyro.CFrame = CFrame.Angles(Direction)

Argument 3 missing or nil
Why i have this error?

1 Like

CFrame.Angles takes 3 arguments, but you’re only giving one.

1 Like

But my Direction is a three-digit value: 0.29635337, 0.864617527, 0.405723006. How do I make it work?

1 Like

Try doing:

BodyGyro.CFrame = CFrame.Angles(0.29635337, 0.864617527, 0.405723006)

I forgot to say, this is the value of my mouse, and it always changes. (I pass it via remote function) trying to write a 3 value in parentheses results in: Unable to cast Vector3 to float

I also thought so at first, but it works even when I don’t click.

local Direction = (Mouse.Hit.Position - Character.Head.Position).unit
BodyGyro.CFrame = CFrame.Angles(Direction)

Argument 3 missing or nil
How do I fix this?

You have to unpack the components of the Direction vector.

BodyGyro.CFrame = CFrame.fromOrientation(Direction.X, Direction.Y, Direction.Z)

Or better yet:

BodyGyro.CFrame = CFrame.new(Vector3.new(0, 0, 0), Direction)
3 Likes

Thanks, it worked, but it didn’t solve my problem with flying :frowning: There have a video above about how my plane flies, the problem has been solved a little, but mostly everything remains as it was. How do I make my plane turn a little slower and more correctly? (My plane is twitching strangely)

My problem is only half solved… I used local Direction = Camera:ScreenPointToRay(Mouse.X, Mouse.Y, 0). Now I can fly a plane, but only in the first person, if I start. control from the 3rd person, my plane turns over on the Z axis so that my head is on the bottom. How do I solve this?