Model changes position after changing orientation

So I’m trying to make a script where a model turns in every direction and fly towards the place it’s looking at. The problem is that model goes to the position (0,0,0) whenever it changes it’s orientation.

Script for the model/AI flying:

local model = script.Parent
while true do
	task.wait()
	model:SetPrimaryPartCFrame(model.HumanoidRootPart.CFrame + model.HumanoidRootPart.CFrame.LookVector *1) 
end

Script for the model/AI changing flying direction:

while true do
	script.Parent:SetPrimaryPartCFrame(CFrame.Angles(math.rad(math.random(-180,180)),math.rad(math.random(-180,180)),math.rad(math.random(-180,180))))
	task.wait(2)
end

Any help is appreciated!

Its Likely the Second Script, you are applying an entirely new CFrame for the model to follow instead, while applying a new Rotation, this may be what you are trying to do?

local Model = script.Parent

local random = math.random
local rad    = math.rad

-- SetPrimaryPartCFrame is Deprecated, use PivotTo Instead

Model:PivotTo(Model:GetPivot() * CFrame.Angles(rad(random(-180, 180)), rad(random(-180, 180)), rad(random(-180, 180)))

However, if you want more randomized results, I recommend you use Random.new to generate numbers, and use radians, like so:

local rmd = Random.new()

rmd:NextNumber(-math.pi, math.pi) -- math.pi is equal to 180 degrees in radians
-- Next Number will return Decimals, use NextInteger if you dont want this effect, which you can remove the math.pi with 180

setting the cframe of a part is setting both orientation and position, if you only use cframe angles you’re skipping the position part and therefore it’s being set to 0,0,0 by default. Use both cframe new and cframe angles

Thanks this really helped, but the AI doesn’t move upwards which is very important.

Also do you think there’s another way of making it so the segments of the body just moves like a flying worm instead of moving/turning the whole model?
(I’m making a worm like AI with multiple segments using ballsocketconstraints)

1 Like

If you’re trying to do what it sounds like you’re trying to do, then theoretically you could have the first segment take the lead and then recursively set the CFrame of every following segment to the previous CFrame of the segment in front of it. For example:

  • At frame one, segment one would have a CFrame of CFrame.new(0, 0, 0) and segment two would have a CFrame of CFrame.new(-1, 0, 0), etc.
  • At frame two, the worm moves forward on the X axis. This makes the CFrame of segment one CFrame.new(1, 0, 0). Segment two’s CFrame becomes CFrame.new(0, 0, 0), the same as segment one’s in frame one, etc.

Won’t this break the joints on the model?

You wouldn’t really need the joints on the model if you were going to do it that way, because the act of recursively setting the CFrame would automatically keep everything together. If you would want to keep them anyways (or for other reasons), then you’d probably be right. Go with what you want to do or what you think works.

Your idea sounds great. But how would I script it?

Edit: Do you actually know a way that I can make it while still using ballsocketconstraints? Because right now it just moves the whole model and doesn’t go upwards. The thing I want is to move only the head and then the segments follow along with it like a worm.