Best way to keep a humanoid in-front of player [Fighting Game]

Hello everyone. I’m trying to keep the enemy I’m attacking in-front of me.

I have tried to use Body Position, Weld and Weld Constraint, Align Position and Align Orientation. But it seems its yanks both of the players.

If anyone has an idea, I’d really appreciate the help.

A simple (albeit slightly naive approach) may be to just use :SetPrimaryPartCFrame() on the enemy model. The following line connected to the RunService.RenderStep event would ensure the Enemy is always 10 studs in front of the player and looking at them too:

--assuming there is a variable `player` which the enemy should clamp to and look at
[EnemyModel]:SetPrimaryPartCFrame(CFrame.new(
   player.Character.PrimaryPart.CFrame * Vector3.new(0,0,-10),
   player.Character.PrimaryPart.Position
))

Better approaches might poll the offset from “in-front of the player” that the enemy currently is, and with some logic decide on an approach to get back to “in-front” (possible flipping over the player if the difference is extreme, while only shifting slightly if its close).
It may also be possible to construct a setup where orientation is always aligned, however default character physics would likely make this buggy, so a custom player controller may be necessary.

1 Like

Connect to RunService’s Heartbeat event, in the event, simply use PivotTo like this:

local attackerCF = attacker:GetPivot() -- or you can use humanoidrootpart
target:PivotTo(attackerCF * CFrame.new(0, 0, -offset) * CFrame.Angles(0, math.pi, 0))

When you need to release the target, disconnect the event.

1 Like

Thank you all for the help. But I have found a way of doing so. I have used Align Position and Orientation correctly this time. Hopefully it works well.