How can I "Clamp" this Properly?

local bodySom = Instance.new("BodyGyro", script.Parent)
bodySom.Name = "wheelie"

while true do wait()
	local value = 45 * script.Parent.CFrame.LookVector.magnitude
	bodySom.CFrame = CFrame.new(script.Parent.Position, script.Parent.Position + script.Parent.CFrame.LookVector) * CFrame.Angles(math.rad(math.clamp(value, -45, 45)), math.rad(0), math.rad(0))
end
  • The Value of the Orientation(x) doesnt get really clamp Correctly.

  • What I want to achieve is that, I want to keep the part from updating from where it is facing and then rotate at a 45° angle, I tried using loop thinking that it is the solution but it just created another problem:

What I Expect To Happen:
image
What Actually Happens:

Sorry my english is kinda bad.

1 Like

So do you want it to stay exactly 45° based on what part it’s “resting” at?

1 Like

Yes, and when I rotate the part it will open/rotate on a 45 angle where it is facing

Not really sure this is what you’re after … best guess.

local bodySom = Instance.new("BodyGyro", script.Parent)
bodySom.Name = "wheelie"

while true do
    task.wait()
    local value = math.clamp(45 * script.Parent.CFrame.LookVector.magnitude, -45, 45)
    local currentRotation = script.Parent.CFrame:pointToWorldSpace(Vector3.new(0, 0, 1))
    local targetRotation = script.Parent.CFrame:pointToWorldSpace(Vector3.new(1, 0, 0))
    bodySom.CFrame = CFrame.new(script.Parent.Position, script.Parent.Position +
      script.Parent.CFrame.LookVector) * CFrame.Angles(0, math.rad(value), 0)
end

Im trying to make a bike wheelie so the while loop is basically to update the BodyGyro’s CFrame to where part/player is facing by using the lookVector.

This is basically what happens(because if you look at the Properties of the BodyGyro > Orientation the value of the x,y,z just keeps adding up, so I tried using “math.clamp()” but it didnt really work):

when you print

value

what is it printing

1 Like

EDIT: This should theoretically work? Not too sure though.

local part = script.Parent
local bodyGyro = part.BodyGyro

while true do
	local ray = workspace:Raycast(part.Position, part.CFrame.UpVector * -20)
	if ray and ray.Instance then
		bodyGyro.CFrame *= (CFrame.new(ray.Position).Rotation * CFrame.Angles(math.rad(45), 0, 0))
	end
	task.wait(.05)
end
1 Like

it just print

45
in the output

have you tried stopping the loop for the wheel

1 Like

Is there a way to like make it rotate like where it is facing like this:
image
What it does:

Im using the loop so that it always update the CFrame. Im trying to make it work in all directions where the part is facing, without the loop it doesnt really update.

just cache the position so no funny business happens

local bodySom = Instance.new("BodyGyro", script.Parent)
bodySom.Name = "wheelie"

local Cframe = script.Parent.CFrame

while true do wait()
	local value = 45 * Cframe.LookVector.Magnitude
	
	bodySom.CFrame = CFrame.new(Cframe.Position, Cframe.Position + Cframe.LookVector) * CFrame.Angles(math.rad(math.clamp(value, -45, 45)), math.rad(0), math.rad(0))
end

https://gyazo.com/4264d015d804d2e4897a78ed731584be.mp4

2 Likes


It Works! but it doesnt really update where the it suppose to be facing…

I figured something else.

local bodySom = Instance.new("BodyGyro", script.Parent)
bodySom.Name = "wheelie"

local UpDir = Vector3.new(0,1,0)

while true do wait()
	local Cframe = script.Parent.CFrame
	local LookVector = -Cframe.RightVector:Cross(UpDir)
	local value = 45 * LookVector.Magnitude
	
	bodySom.CFrame = CFrame.new(Cframe.Position, Cframe.Position + LookVector) * CFrame.Angles(math.rad(math.clamp(value, -45, 45)), math.rad(0), math.rad(0))
end

if you want to make it accurate on steep surfaces just change the UpDir with the ray.Normal of a ray with direction -Y (aka workspace:RayCast(script.Parent.Position, Vector3.new(0,-5,0))

1 Like

Alright! Thank you for helping :smiley:

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