Setting a CFrame to a Position with specific Orientation and then changing it

I’m currently experimenting with an old raycast car project I used to work on, and I’m stuck at CFraming the wheels to be above the ground and rotate.

The wheels have to be in line with their respective attachment (e.g. front attachments can rotate, thus the wheels turn around yaw axis). But they also have to turn around the pitch axis as the vehicle moves forward/backward.

This is where I simply got stuck:

  1. Set the Wheel’s position to Attachment.WorldPosition
  2. Align only the wheel’s Y-orientation with the attachment’s Y-orientation
  3. Rotate the wheel by RotValue (deg.) on its pitch.
  4. Repeat

This is what I tried, and it doesn’t work regardless if its placed in the X or the Z position, so I’m open and will appreciate any help:

Spring.Wheel.CFrame = CFrame.new(StaticSpringAttachment.WorldPosition) * CFrame.fromOrientation(math.rad(Spring.Wheel.Orientation.X), math.rad(StaticSpringAttachment.WorldOrientation.Y), math.rad(Spring.Wheel.Orientation.Z))
local RotValue = (((Spring.Wheel.Position - LastPositions[Spring.Name]).Magnitude) / circ) * 360;
Spring.Wheel.CFrame *= CFrame.Angles(0, 0, math.rad(RotValue))

1 Like

Solved.

Here if anyone runs into this issue in the future:

-- Assuming Weld Part0 is PhysicsObject, Part1 is WheelMesh

local RotValue = (((StaticSpringAttachment.WorldPosition - LastPositions[Spring.Name]).Magnitude) / circ) * 360;
local weldInitPos = CFrame.new(StaticSpringAttachment.Position) * CFrame.new(0, -currentLength, 0)
			
if string.sub(Spring.Name, 1,1) == "F" then
	WheelWeld.C0 = weldInitPos * CFrame.Angles(0, math.rad(StaticSpringAttachment.Orientation.Y), 0)
else
	WheelWeld.C0 = weldInitPos
end

WheelWeld.C0 *= CFrame.fromAxisAngle(Vector3.new(1,0,0), -math.rad(RotValue))

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