Changing NPC's C0s to face target position on y-axis

  1. What do you want to achieve?
    I have been working a gun NPC for a while, I was able to get the npc to face the target on the y-axis, however it didn’t fully work as expected since I later realized that it also changes based on the targets orientation on the y-axis.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have been trying out various different solutions on the developer forum and with the different methods within CFrame, none of which solve or really helped. Some of the solutions I have tried uses the CFrame.lookAt(), however the parts tend to move infinitely in a certain direction, or set a new position way off from where the npc is supposed to be. If there are any that I may have not considered please let me know. Thank you in advance.

  3. What is the issue? Include screenshots / videos if possible!
    Sorry for the low quality video.
    The npc can follow the target, but it only follows momentarily, only when you jump it follows. Also through testing, what i don’t understand is why it only follows the target when its unanchored.

This was the code I was using

if math.abs(UpperTorso.Position.Y - root.Position.Y) > 1 then
--print("Player's height is greater than 1")
	local leftMotor6 = npc:WaitForChild("LeftUpperArm"):WaitForChild("LeftShoulder")
	local rightMotor6 = npc:WaitForChild("RightUpperArm"):WaitForChild("RightShoulder")
	local headMotor6 = npc:WaitForChild("Head"):WaitForChild("Neck")

	leftMotor6.C0 = CFrame.new(leftMotor6.C0.Position) * CFrame.Angles(-UpperTorso.CFrame.LookVector.Y, 0, 0)
	rightMotor6.C0 = CFrame.new(rightMotor6.C0.Position) * CFrame.Angles(-UpperTorso.CFrame.LookVector.Y, 0, 0)
	headMotor6.C0 = CFrame.new(headMotor6.C0.Position) * CFrame.Angles(-UpperTorso.CFrame.LookVector.Y, 0, 0)
end
4 Likes

Is the code ran inside a while loop or loop of some sorts? Why is there an if statement checking the players height?

1 Like

Hello, this is a very annoying issue, as I’ve experienced it multiple times as well. This has something to do with “retargeting”. It’s a property of workspace and is turned on by default. Turn it off and see if you get any results. If not, then your code is broken. (You can put prints in the if statement and see if the C0s get edited)

1 Like

Yes, its actually in a runservice.Heartbeat, and its checking the height so that if the player’s on a different level the npc should orientate its arms and head towards the target.

1 Like

Hello, I tried it out, it unfortunately didn’t work, but thanks for the considerations. Also,


I checked the C0s orientations, I actually found that its constantly changing.

I would move the constants that use wait for child to outside the loop. This will ensure they are not being created each time, which is inefficient and could cause delays. I’m not sure this will work but it will make your code run faster.

It unfortunately didn’t change anything, but thanks for the advice though.

Then it is probably is a problem with this part of your code. I’m not very experienced with Cframes but maybe someone else can say what’s wrong with this.

I agree, but thanks for trying to help.