Trying to rotate a model towards a location and move it in that direction

I am trying to make some sort of basic underwater AI, and because it’s underwater I can’t use Pathfinding service or MoveTo() because they can’t go down in water. Anyway, I’m trying to apply a body gyro and a body thrust, but its not moving or rotating in the direction it should be

local dest = Vector3.new(math.random(-speed,speed),math.random(-speed,speed),math.random(-speed,speed))
local orientation = CFrame.lookAt(humanoidRootPart.Position,humanoidRootPart.Position + dest)
orientation = orientation.LookVector
humanoidRootPart.BodyGyro.CFrame = CFrame.Angles(orientation.X,orientation.Y,orientation.Z)
humanoidRootPart.BodyThrust.Force = dest

I’m not very good with CFrames/body movers, so it completely confuses me why they move the model just not in the right direction.

You don’t have to use the LookVector for BodyGyros. They can take any raw CFrame, they just ignore the position. The reason this isn’t working right now is because you’re creating a new CFrame using the LookVector as angles for CFrame.Angles. LookVector is more of a unit vector that tells the facing direction of something while CFrame.Angles expects three angle inputs in radians, as if you were setting the orientation of something using radians instead of degrees. This should work:

local dest = Vector3.new(math.random(-speed,speed),math.random(-speed,speed),math.random(-speed,speed))
local orientation = CFrame.lookAt(humanoidRootPart.Position,humanoidRootPart.Position + dest)
humanoidRootPart.BodyGyro.CFrame = orientation
humanoidRootPart.BodyThrust.Force = dest

Sorry for the late reply, i was eating-

Anyway, this does rotate the model, but the rotation is still not synced up to the body thrust movement, which leads to this weird situation where the model will turn and then just start moving in some completely different direction.

This sounds like a classic problem where the front side of the model isn’t actually the front face in Roblox, in that case you can just rotate the orientation variable until it matches up. Try this:

local orientation = CFrame.lookAt(humanoidRootPart.Position,humanoidRootPart.Position + dest) * CFrame.Angles(0, math.rad(90), 0)

If it’s still wrong, try switching out the 90 with 180 or 270. And if that fails, try using math.rad on other axes instead, it might not be the y-axis that’s rotated wrong. That’s just the most commonly messed up axis for facing direction issues.

(once again, sorry for the late reply)

270 on the Y axis makes it rotate as expected, but it’s still not moving in that direction. Is there anyway i could apply a general “forward” motion?

BodyThrust is kind of odd, I never really worked with them. I usually use BodyVelocity for this sort of thing. If you make a BodyVelocity and set its velocity to the orientation.LookVector * (some speed) then it should work.

Well this almost works, but the Y axis of the BodyVelocity is remaining 0, making it just kind of float around.

Is there anything else keeping it in air? Maybe the MaxForce is uneven?

Max force is 4000, 4000, 4000, and the only other body mover on it is the BodyGyro being used to rotate it

Maybe try using dest.Unit * speed instead of orientation.LookVector, should be the same but maybe something is off?

that works! i have no idea why the other one wouldn’t work, but its working now!

thanks!

1 Like

Try this

local destOffset = Vector3.new(math.random(-speed,speed), math.random(-speed,speed), math.random(-speed,speed))

local destination = humanoidRootPart.Position + destOffset

local difVector = destination - humanoidRootPart.Position

local lookAtCFrame = CFrame.lookAt(humanoidRootPart.Position, destination)

humanoidRootPart.BodyGyro.CFrame = CFrame.Angles(lookAtCFrame:ToEulerAnglesYXZ())
humanoidRootPart.BodyThrust.Force = humanoidRootPart.CFrame:VectorToObjectSpace(difVector.Unit * forceFactor)