Help aiming a model without rotating on the x or z axis?

I have a script that constantly set the cframe of a model among other things. but I noticed that when it reaches its goal the model rotates slightly on the x or z axis right at the end. this is somewhat ugly having the model spasm when its done moving. here is the script:

function goto()
	local mover = script.Parent.Parent.Parent.Center.BodyVelocity
	local center = script.Parent.Parent.Parent.Center
	
	repeat
	wait()
	script.Parent.Parent.Parent:SetPrimaryPartCFrame(CFrame.new(center.Position,script.Parent.Value))
	mover.Velocity = script.Parent.Parent.Parent.Center.CFrame.LookVector * 10
	until center.Position.X < script.Parent.Value.X + 0.7 and center.Position.X > script.Parent.Value.X - 0.7 and
	center.Position.Z < script.Parent.Value.Z + 0.7 and center.Position.Z > script.Parent.Value.Z - 0.7
	
	
	mover.Velocity = script.Parent.Parent.Parent.Center.CFrame.LookVector * 0
end




script.Parent.Changed:Connect(function(newval)
	if newval ~= Vector3.new(0,0,0) then
		goto(newval)
	end
end)

I know CFrame has properties such as cframe.z or cframe.x. but I am unsure on how to only set the models cframe angle using only the Y axis. could someone show me the right way?

Instead try CFrame lerping the lookAt constructor on CFrame (It’s deprecated, but it’s the fastest way).

local value = 0
local steppedListener
steppedListener = RunService.Stepped:Connect(function(step)
	value = value + step
	if value <= 1 and (otherPart.CFrame.p - part.CFrame.p).magnitude >= 0.7 then
		part.CFrame = part.CFrame:lerp(CFrame.new(part.CFrame, otherPart.CFrame) + CFrame.new(part.CFrame - otherPart.CFrame), value) -- lerps it to look at and go towards them.
	else
		steppedListener:Disconnect()
	end
end)

if its depreciated its subject to removal at any time. are you sure I should be using it?

If you want, you can use this:


function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    local upVector = Vector3.new(0, 1, 0)
    -- You have to remember the right hand rule or google search to get this right
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
 
    return CFrame.fromMatrix(eye, rightVector, upVector2)
end

Just change the constructor to this function.

Found it here: CFrame | Documentation - Roblox Creator Hub

could you show me how its put in? from what I understand the function you provided goes above the GoTo() function, if this is the case then were would your function go?

Ok, I’ve got a final codebase.


local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    local upVector = Vector3.new(0, 1, 0)
    -- You have to remember the right hand rule or google search to get this right
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
 
    return CFrame.fromMatrix(eye, rightVector, upVector2)
end

local function twoValue(a,b,t)
    return a + (b - a) * t
end

local function lerp(a, b, step)
    local x1, y1, z1, x2, y2, z2 = a.CFrame:ToEulerAnglesXYZ(), b:ToEulerAnglesXYZ()
    return CFrame.new(a.Position + ((b - a.Position) * step)) * CFrame.Angles(twoValue(x1, x2, step), twoValue(y1, y2, step), twoValue(z1, z2, step)
end

local steppedListener
local function movePart(part1, value, threshold)
    steppedListener = RunService.Heartbeat:Connect(function(step)
        if (part1.CFrame.Position - value).magnitude >= threshold then
            part1.CFrame = lerp(part1, lookAt(part1, value) + CFrame.new(part2.Position - part1.Position), step)
        else
            steppedListener:Disconnect()
        end
    end)
end

Then you’ll do:

script.Parent:GetPropertyChangedSignal("Value"):Connect(function()
	local value = script.Parent.Value
	
end)

so I found that using CFrame and a bodygyro makes it a lot simpler to accomplish what I need, however what you have will still come in handy and since it also solves the problem I will mark it as the solution.

1 Like