Currently, I’m trying to make an AI that moves away from the player, but still looks at the player.
Since I use the MoveTo() function when I have the AI move away from the player, I think it moves to a different position every time, meaning it wouldn’t look at the player, but instead facing where the new position is.
I’ve tried looking around the internet, but the post solution either doesn’t work with my script, or it is very glitchy. (It isn’t really as smooth as I would like it to be.)
Here’s the code I use to move the AI away from the player:
(By the way, Target is the HumanoidRootPart of the player’s character, RunDistance is just a number (10), and HumanoidRootPart is the AI’s HumanoidRootPart.)
local function RunAway()
if Target ~= nil then
local PositionDifference = Target.Position - HumanoidRootPart.Position
local NormalizedDirection = PositionDifference.Unit
Humanoid:MoveTo(HumanoidRootPart.Position + (Target.Position - HumanoidRootPart.Position) * -1 * RunDistance)
else
return nil
end
end
Thank you for your help! This is my first forum post, so if anything isn’t clear, I can help explain it
Also you should learn about Pathfindingservice so that if your NPC gets stuck between a wall or the player is too high, the NPC can still get to the player.
It works, but is there a way to make it smoother? It takes a slight second to update and look at the player, plus it messes with the movement code a little bit.
I tried using AlignOrientation, (because that seemed like the one that would work best with my code,) but disabling humanoid AutoRotate seems to not let my AI rotate at all for some reason. Is there a reason why for this, or is my code just broken? (I saw the code on a forum post while searching)
This is the second time I have seen you not only misinterpreting what the poster wanted, but providing deprecated methods. Please only provide help if you actually can.
As for the original post, the Model or Actor that contains the NPC can be rotated by using the Pivot to function.
Model:PivotTo(CFrame.new(CurrentCFrame, TargetPosition))
The target position will be where it will look at.
You must disable Humanoid.AutoRotate when you are running this code, and re-enable it when you are not.
The smoothest you can have is a physics constraint, but those can fail for seemingly Jo reason. So the next best option is to set the CFrame of the AI’s Root part
That’s interesting! I’ve tried it out (see code below), but I’m not the best at CFrame.lookat or Lerp. It works fine, but my AI waits for a split second, then faces the player and teleports to a location. Is there any way to improve on this piece of code? Thank you for the idea though! It seems really helpful
local LookAt = CFrame.lookAt(HumanoidRootPart.Position, Target.Position)
local Alpha = 0
local InterpolatedCFrame = LookAt:Lerp(Target.CFrame, Alpha)
HumanoidRootPart.CFrame = InterpolatedCFrame
Your alpha is how much you want to interpolate it. If alpha is 0, you get the first cframe. If alpha is 1, you get the second cframe. If alpha is between, you get a cframe between both cframes