NPC walk while facing a point

I need a NPC to face towards a point while walking in another direction as if it’s using mouselock

My end goal is to get a NPC to sword fight like in the good ol days in sword fighting tournament.
EX: you walk with mouselock on and stab with your sword while moving to the side where your opponent has no sword

I have tried CFrame - Super Choppy

script.Parent.HumanoidRootPart.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, target.Character.ClassicSword.Handle.Position * Vector3.new(1,0,1) + script.Parent.HumanoidRootPart.Position*Vector3.new(0,1,0))

and i have also tried bodyGyro - Which doesn’t turn fast enough for it to work properly

script.Parent.HumanoidRootPart.BodyGyro.CFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, target.Character.ClassicSword.Handle.Position * Vector3.new(1,0,1) + script.Parent.HumanoidRootPart.Position*Vector3.new(0,1,0))

Is there another way to do it by creating some sort of NPC mouselock?? Help appreciated.

1 Like

If your problem with using BodyGyros is the fact that it turns too slow, you can increase the MaxTorque and P properties on the BodyGyro until you get the desired rotation speed.

1 Like

I’ve tried doing that, and increased the numbers to 7 septillion, but it doesn’t really change it at all. I’ve also tried using math.huge but then it just makes the character slingshot before i even get to see it at all.

I think the walking mechanics might be overpowering the bodygyro??? - Just a thought

you could tween the models PrimaryPart CFrame to this position. (tweening it should remove the choppy feeling)

local targetPoint = Vector3.new(target.PositionX, npcRoot.Position.Y, targetPosition.Z)
npcRoot.CFrame = CFrame.new(npcRoot.Position, targetPoint)

this will make the NPC face something. (without rotating the Y axis)
might have to change it a bit IF your orientation is different.

I tried to tween the NPC to look at the sword, but now all it does is looks at the sword.

In other words my NPC cannot walk anymore with :moveto

Does this mean that i will have to create my own version of walkspeed with the tweening service?

then you’ll probably have to use a bodygyro (within the models PrimaryPart) and use similar code.

bodygyro.CFrame = CFrame.new(npc.Position - target.Position)

Does your NPC have a humanoid part? If so check this out: Humanoid | Documentation - Roblox Creator Hub

You can make your NPC move using one of two functions.

humanoid:Move() -- Will move in 1 direction once its facing it
humanoid:MoveTo() -- Will move to a specific location

Try either of those out.

Another service that I’ve never worked with, but one you can try is this: PathfindingService | Documentation - Roblox Creator Hub

Don’t know if either of these will help you.

Alternatively, you can literally just tween the humanoid very fast in the direction wanted using CFrame. That way it simulates a real movement that looks like shiftlock.

Yes, my NPC has a humanoid root part

I have been using MoveTo this entire time, while randomly testing Move if it works also but when i am tweening the cframe like the other guy said it makes the character turn perfectly, although it makes the character not able to walk.

I belive the pathfinding service doesn’t make it move, rather just finds waypoints for you to make your character move towards with MoveTo, thus making it not any better for my scenario

Why not try Character:SetPrimaryPartCFrame() instead? Not sure if it will work but it very well could.

If you uncheck the AutoRotate property of the humanoid then the Humanoid will stop trying to make the character face the direction it is walking in. You are then free to use BodyGyro to make it face whatever direction you want. If you need it to spin faster just decrease the D property of BodyGyro

4 Likes

I gave character:SetPrimaryPartCFrame() a shot and it was the same as humanoidrootpart.CFrame
it makes it’s movement very choppy

When i tried that my character wasn’t rotating at all, and i decreased the D property to 0 and increased the max force by a lot and played with the P settings and it doesn’t turn at all. it just stays in the same position unless it bumps into a wall and the wall makes it rotate

Is the max force set very high on the Y axis? If it rotates freely when it hits a wall it seems to me that the gyro is not putting any force on it. Could you send us a picture of the character in your explorer window and show us a picture of the properties of the gyro and humanoid?

Annotation 2020-04-04 122249

Annotation 2020-04-09 133627
Here is the bodygyro and humanoid properties and the character in the explorer

Note: i dont use that localscript, i deleted it right now because it’s useless

I found your problem. In your BodyGyro the MaxTorque for the Y-axis is set to zero. This is preventing the gyro from rotating the character in the direction you want it to.

Set the MaxTorque property to Vector3.new(0, math.huge, 0) and then it should rotate in the direction you tell it to.

1 Like