Trying to make a NPC that will follow a part facing it similar to a goalkeeper in these two games.
I don’t want the NPC to come out of the goalpost/forward, I just want it to move left or right. I’m also trying to use CFrame since it has better physics.
for it to only move right and left, when making it move only change the X value (or Z depending on orientation of the pitch)
so to make it move 1 stud to the right, multiply cframe by CFrame.new(1,0,0)
and make the 1 negative to move it one stud to the left
and to make sure the goalie doesnt move out of the goal, you can clamp the variable with math.clamp
--[[quick example]]--
--lets say 0,0,0 is the middle of the pitch.
local ballCFrame = CFrame.new(5,0,-4)
-- the ball was kicked to the right and forward
local goalie = --reference to goalie : Model
goalie:PivotTo(CFrame.new(ballCFrame.X, 0,0))
--only move the goalie on X axis
--[[Complicated - Math.Clamp]]--
local ballCFrame = CFrame.new(5,0,-4)
local goalie = --reference to goalie : Model
local min = 3 -- so the goalie doesn't move out the goal
local max = 5 -- ^^
local x = math.clamp(ballCFrame.X, min, max)
-- if the X value is more then the goal, then the goalie will stay at the edge of the goal. If it is less than the goal, goalie stays at other edge.
goalie:PivotTo(CFrame.new(x, 0,0))
Obviously this would be wrapped in a loop so it moves the goalie constantly
Whenever I play the game, the goalie just teleports to a different place. So how could I make it so the goalie would stay at the place it was originally at.