I need to script a goalkeeper for my soccer (football) game.
What I need someone to teach me is how to align the goalkeeper (npc, dummy) with the ball.
For example, the ball is in the middle, the goalkeeper will also be in the middle of a selected region.
same for left, right, and anywhere. Something like the goalkeeper in the game “Super Striker League”:
One way would be to use the CFrame of the ball to determine where the goal keeper should be.
local function update()
-- Get the goal keeper and make sure it is active.
local gk = workspace.GoalKeeper
if not gk or not gk.Parent then return end
-- Get the ball's position and the goal keeper's position.
-- Use the y-position of the ball to determine the location of the goal keeper.
local ballPos = ball.Position
local gkPos = gk.Position
gkPos = Vector3.new(gkPos.X, ballPos.Y, gkPos.Z)
-- Move the goal keeper to the calculated position.
gk.CFrame = CFrame.new(gkPos)
end
-- Run the update loop every 0.1 seconds.
while true do
update()
wait(0.1)
end
just because somethign wasn’t defined doesn’t automatically mean that its ChatGPT generated, but I also get the feeling that this was generated by ChatGPT purely because of the structure and comments.
Also, if you want it to be like Super Striker League, you’ll need a bounding box, which you can sort of create using math.clamp
set which CanCollide to off?
if you set the part’s CC to off, then the goalkeeper can walk through?
if you set the ball’s CC to off, then it’ll just fall through the floor…
if you set the player’s CC to off, it’ll also fall through the floor.
you’ll need to make use of collision filtering
What do you mean the goalkeeper can walk through, that’s what I want. I want to goalkeeper to walk to align itself with the ball while inside a box, if it can’t walk through it’s stuck.
Ohh, I get what you mean, only I don’t want it to look like he’s running into a wall. Have you watched the video I put above? It shows exactly what I’m trying to acheive and it doesn’t look like it’s running into a wall.
local NPC = script.Parent
local ball = workspace:WaitForChild("Ball")
local region = workspace:WaitForChild("Region")
while true do
local ballPos = ball.Position
local regionPos = region.Position
if ballPos.Z < regionPos.Z - region.Size.Z/2 then
NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z - region.Size.Z/2)
elseif ballPos.Z > regionPos.Z + region.Size.Z/2 then
NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, regionPos.Z + region.Size.Z/2)
else
NPC.HumanoidRootPart.CFrame = CFrame.new(regionPos.X, regionPos.Y, ballPos.Z)
end
wait(0.05)
end
This script above worked, but the goalkeeper is rotated 90 degrees to the wrong side and I want to rotate it 90 degrees to the left, how do I do that?
I’ve tried doing it manually, but that won’t work.
by “manually”, does that mean you tried applying an offset during the script? Also, “while true do” is not recommended (though I’m not sure if there’s an alternative - perhaps only run it if the ball is moving?) Just do an offset of of a few degrees, and print out the direction / offset to see if it works…
Why don’t you check the balls position on a axis and then tween the goalkeeper to the axis. In order to see if the ball is close you can check the magnitude and see if its greater than the maximum distance or not. In order to stop the goal keeper from leaving his net you can set a axis limit on the axis and see if the balls axis is greater than the maximum axis and of it is you can tween the goalkeeper to the maximum axis.