How would i change the *ROTATION ONLY* through CFrame?

Im trying to make an “Auto target” system that automatically turns your character toward the nearest enemy. i tried to do this by setting the humanoidrootpart’s cframe to CFrame.lookAt(their current position here, the enemy position here). what i think this is supposed to do is keep them in the same position, but turn their character to look at the nearest enemy. when i tested it, however, the character started glitching a lot. i think this is due to the cframe being slightly off with the position, so i want a way to change the rotation only. does anyone know a way to do this, or possibly another thing to use other than cframe to accomplish the goal?

1 Like

what exactly happens with CFrame.lookAt??

screenshot pls??

also,

CFrame.Rotation

1 Like

cframe.lookat basically sets the position to the first vector3 (in this case, the characters current position) and an orientation that looks at the second vector3 (in this case, the enemy position)

1 Like

also what does cframe.rotation do?

1 Like

You have to make sure in the second argument that the Y component of the Vector3 is the characters Y position

1 Like

can i see your code? i don’t think it should be breaking

boy i truly wonder what a property of CFrame called “Rotation” does

In this case enemy would be a part but in your case it’s probably a model, you’d just make it enemy.PrimaryPart or enemy.HumanoidRootPart

local root = character:WaitForChild("HumanoidRootPart")

local enemy : Part = workspace:WaitForChild("Enemy")
local targetPosition = Vector3.new(enemy.Position.X, root.Position.Y, enemy.Position.Z)

root.CFrame = CFrame.lookAt(root.Position, targetPosition)


here is the problem im having. its glitching out the position

Actually I know the issue but it would be cool if you could show the code too, basically you dont want to set the players root directly you want to use a constraint or a bodygyro

BodyGyro is deprecated but in my opinion it’s faster too add into your workflow

local turn = false
local char = script.Parent.Parent
local hrp = script.Parent.Parent:WaitForChild("HumanoidRootPart")
local mm = require(workspace.MainModule)
local results
local enemyPos
local enemyChar
local distanceFromEnemy
char.AutoTarget.Changed:Connect(function(value)
	print(value)
	if value == true and char:WaitForChild("Humanoid").Health > 0 then
		results = mm.findClosestPlr(char:WaitForChild("Head"), "Player")
		if results ~= nil then
			enemyChar = results[1]
			enemyPos = enemyChar.Torso.Position
			distanceFromEnemy = results[2]
		end
		for count = 0, 25,1 do
			hrp.CFrame = hrp.CFrame:Lerp(CFrame.lookAt(hrp.Position, enemyPos), count/25)
		end
		turn = true
	else
		turn = false
	end
end)
repeat
	if turn == true and enemyPos ~= nil then
		hrp.CFrame = CFrame.lookAt(hrp.CFrame.Position, enemyPos)
	else
		print(turn)
	end
	task.wait(.05)
until false

heres my code. the problem is mostly in the loop on the bottom

Yeah you dont wanna do this, you want to use a constraint or a body mover, setting the players hrp directly like that leads too issues like this

1 Like

okay ill try it, ill let you know if it works

1 Like

thanks, i used alignorientation and it worked perfectly.

1 Like

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