Math stuff Help!

I only know middle school math for an obvious reason but I want to make my characters head move to point at player cursor.

I thought of how the C0 will be 0, 0, 0 despite the orientation of the character and how Cframe:lookat() will only work for things on the base axis. So I thought of subtracting the heads orientation from the C0 orientation and then adding the CFrame:lookat() value to the C0 orientation. I tried this in command bar and it give error about the orientation value needing to be a vector3. I just want to know if my idea would work in the first place to be honest. Thanks

1 Like

CFrame:lookat() uses either a CFrame or a Vector3, don’t know which because I don’t use these often. Look up “how to point part towards something roblox” and find some help pages/video tutorials, I’m sure there are quite a few on this.

1 Like

The thing with using a neck’s motor6d is that the orientation is on its own axis so I was wondering if subtracting the orientation of the character’s head from the motor6d c0 orientation would result in the head’s physical rotation would be equal to something with an orientation of 0, 0, 0 in the normal axis

1 Like

This is why I can’t just use CFrame:lookat() because it will return a value that would only work on a normal axis which is why I want to know if my idea of cancelling out the orientation to look like 0, 0, 0 and then adding the CFrame:lookat() value to the orientation would work

1 Like

Motor6Ds are just an offset value, they are in local space/object space; you have to create a world space CFrame and then translate that to object space then apply it to the neck c0

Of course… you can add angle limits

local roblox = workspace.Roblox
local neck = roblox.Head.Neck
local block = workspace.block

game:GetService("RunService").Heartbeat:Connect(function(dt)
   local objectspace = neck.Part0.CFrame:ToObjectSpace(CFrame.lookAt(neck.Part0.Position, block.Position))
   neck.C0 = CFrame.new(neck.C0.Position) * objectspace
end)
2 Likes

You will need to use a CFrame.lookat() and use some math to change it from global space to local space

1 Like

how do i limit it, i try using math.clamp but i can still turn 180 degrees despite limit to 45

1 Like

Math.clamp should work. Just make sure to work with the objectspace CFrame and make sure the angles units are in radians.

1 Like

i did what you did but it doesnt limit?

-- services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- variables
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Character, Neck, Head

local function SetCharacter(Char)
	Character = Char
	Head = Character:WaitForChild("Head")
	Neck = Head.Neck
end

if Player.Character  then
	SetCharacter(Player.Character)
end
Player.CharacterAdded:Connect(SetCharacter)

-- code
RunService.RenderStepped:Connect(function()
	local ObjectSpace = Neck.C0:ToObjectSpace(CFrame.lookAt(Neck.C0.Position, Mouse.Hit.Position))
	local x, y, z = ObjectSpace:ToOrientation()
	-- limit rotation
	x = math.clamp(x, math.rad(-45), math.rad(45)) 
	y = math.clamp(y, math.rad(-45), math.rad(45))
	z = math.clamp(z, math.rad(-45), math.rad(45))

	Neck.C0 = Neck.C0 * CFrame.fromOrientation(x, y, z)
	wait(0.1)
end)

Should be like this:

RunService.RenderStepped:Connect(function()
	local ObjectSpace = Neck.Part0.CFrame:ToObjectSpace(CFrame.lookAt(Neck.C0.Position, Mouse.Hit.Position))
	local x, y, z = ObjectSpace:ToOrientation()
	-- limit rotation
	x = math.clamp(x, math.rad(-45), math.rad(45)) 
	y = math.clamp(y, math.rad(-45), math.rad(45))
	z = math.clamp(z, math.rad(-45), math.rad(45))
	Neck.C0 =  CFrame.fromOrientation(x, y, z) + Neck.C0.Position
end)
1 Like

it still spinny crazy char char i did what you said to do

although it seems to be limited so that is good. I dont know much about cframes so sorry if some of this should be common knowledge

Ok i just added a lerp and it seems to work fine :grin:

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