Help with keeping part upright

I am trying to rewrite a Unity code in LUA but I’m having trouble rewriting it. My current code doesn’t react the same as the Unity code.

Unity Code:

public void UpdateUprightForce()
{
	Quaternion characterCurrent = transform.rotation;
	Quaternion toGoal = UtilsMath.ShortestRotation(_uprightJointTargetRot, characterCurrent);
	
	Vector3 rotAxis;
	float rotDegrees;
	
	toGoal.ToAngleAxis(out rotDegrees, out rotAxis);
	rotAxis.Normalize();
	
	float rotRadians = rotDegrees * Mathf.Deg2Rad;
	
	_RB.AddTorque((rotAxis * (rotRadians * uprightJointSpringStrength)) - (_RB.angularVelocity * _uprightJointSpringDamper))
}

My Code:

local defaultRot = CFrame.Angles(0, 0, math.rad(90))
local uprightJointSpringStrength = 50
local uprightJointSpringDamper = 10

local function UpdateUprightForce()
	local characterCurrent = RootPart.CFrame
	local toGoal = defaultRot:ToObjectSpace(characterCurrent)
	
	local rotAxis, rotRadians = toGoal:ToAxisAngle()
	local rotDegrees = math.deg(rotRadians)
	rotAxis = Vector3.new(rotAxis.X, rotAxis.Y, rotAxis.Z).Unit

	local torque = (rotAxis * (rotRadians * uprightJointSpringStrength)) - (RootPart.AssemblyAngularVelocity * uprightJointSpringDamper)
	RootPart:ApplyAngularImpulse(torque)
end

I’m following this video tutorial: https://www.youtube.com/watch?v=qdskE8PJy6Q&t=146s&ab_channel=ToyfulGames

in Roblox there’s something called an AlignOrientation, use that instead

If you don’t want to use it, try

local defaultRot = CFrame.Angles(0, 0, math.rad(90))
local uprightJointSpringStrength = 50
local uprightJointSpringDamper = 10

local function UpdateUprightForce()
    local characterCurrent = RootPart.CFrame
    local toGoal = defaultRot:inverse() * characterCurrent -- Equivalent to UtilsMath.ShortestRotation(_uprightJointTargetRot, characterCurrent)
    
    local rotAxis, rotRadians = toGoal:toAxisAngle()
    local rotDegrees = math.deg(rotRadians)
    rotAxis = Vector3.new(rotAxis.x, rotAxis.y, rotAxis.z).unit -- Adjusting for differences in vector notation

    local torque = rotAxis * (rotDegrees * uprightJointSpringStrength) - RootPart.AssemblyAngularVelocity * uprightJointSpringDamper
    RootPart:ApplyTorque(torque)
end

Uhh. I tried adjusting the stiffness and the damping but it doesn’t really fix this issue:

im so confused now, what exactly are you using

I tried the script you provided.

I honestly have no idea atp sorry

Hm. This should be possible with two constraints, a SpringConstraint, and a PrismaticConstraint.

But those will be laggy and not very good than code.

This is very wrong. Constraints are very optimized, programming physics will definitely be more taxxing and less optimized, especially if you don’t know what you’re doing. Use the constraints.

Oh. Maybe some other time but I want to fix this problem.

Yes, using constraints will fix your problem. They’re the simplest thing.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.