CFrame.LookAt() is causing weird teleportation issues

I have a script that makes an npc face a player before playing an animation. I use CFrame.LookAt() to change his direction, but for some reason, the code causes the dummy to teleport into the void under the map.

local pos = CFrame.lookAt(character.PrimaryPart.Position,Vector3.new(attacked.Character.PrimaryPart.Position.X,character.PrimaryPart.Position.Y,attacked.Character.PrimaryPart.Position.Z))
character:PivotTo(pos)

character is the npc, and attacked is the player

2 Likes

Try

local attackedCharacter = attacked.Character
local attackedPivotPosition = attackedCharacter:GetPivot().Position
character:PivotTo(CFrame.lookAt(attackedPivotPosition, Vector3.new(attackedPivotPosition.X, character:GetPivot().Position.Y, attackedPivotPosition.z)))

That makes the character teleport to the attackedCharacter. I want him to stay in place and just look at him

My bad,

local characterPivotPosition = character:GetPivot().Position
local attackedPivotPosition = attacked.Character:GetPivot().Position
character:PivotTo(CFrame.lookAt(characterPivotPosition, Vector3.new(attackedPivotPosition.X, characterPivotPosition.Y, attackedPivotPosition.Z)))

Hey, to fix this you can just do:

character:PivotTo(CFrame.new(character.PrimaryPart.Position, attacked.Character.PrimaryPart.Position))

It still teleports him under the map

Did you try my solution or EcoCrashed’s?

Yours makes him teleport under the map. Eco’s makes him face the attacked, but if the attacked happens to be at a higher elevation, it makes him fall over

Wait, this is my code how is that so?

A great way to do it is instead of using CFrame.LookAt, I do:

Enemy.HumanoidRootPart.CFrame = CFrame.new(Enemy.HumanoidRootPart.Position, Target.HumanoidRootPart.Position)

I normally don’t use CFrame.LookAt. Totally not because I’ve never really used it.

I’m pretty sure those do the same thing.

It does, tested his code out as well, and it seems to work the exact same way. Did you forget to set the primary part? If not, can you try character.PrimaryPart.CFrame = pos instead of character:PivotTo(pos)? It should just error if there is no primary part.

Yeah, I didn’t doubt you lol but why not just use PivotTo()

Hmm, I ran a few tests, and your code seems to work if this part of the script isn’t running.

character.RightHand:FindFirstChildWhichIsA("Motor6D").Enabled = false
character.RightHand.Anchored = true
character.RightHand.CFrame = character.PrimaryPart.CFrame * CFrame.new(2,2,-2)
local handtween = game:GetService('TweenService'):Create(character.RightHand,TweenInfo.new(.5,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,true),{Position = attacked.Character.PrimaryPart.Position})
handtween:Play()
touchable = true
handtween.Completed:Wait()
character.PrimaryPart.Anchored = true
touchable = false
character.RightHand.Anchored = false
character.RightHand:FindFirstChildWhichIsA("Motor6D").Enabled = true
character.PrimaryPart.Anchored = false

Is there anything in there that could somehow break him?

What’s the chance that the character’s and the target’s X and Z positions happen to be the same? If that happens, CFrame.lookAt will provide an unexpected result of NaN because you can’t really make one position look at itself

local pos = CFrame.lookAt(character.PrimaryPart.Position,Vector3.new(attacked.Character.PrimaryPart.Position.X,character.PrimaryPart.Position.Y,attacked.Character.PrimaryPart.Position.Z))
if pos == pos -- NaN isn't equal to itself
	character:PivotTo(pos)
end

That makes sense, but no, they are at different X and Z values.