Kicking a Soccer Ball Tutorial

GUIDE TITLE: Kicking a Soccer Ball

ESTIMATED COMPLETION TIME: 30-40 Minutes recommended

CORE VERSION: 2.2.33.1

SUGGESTED PREREQUISITES: Lua scripting basics would help understand the provided code.

TUTORIAL SUMMARY:

Learn how to kick a soccer ball using Abilities, Physics Objects, and Lua scripting.

EXPECT TO LEARN:

  • Creating an Ability
  • Creating a Static Player Equipment
  • Creating a Physics Object
  • Creating a Kill Zone
  • Lua scripting
    • Checking ability phase
    • Checking trigger collision
    • Broadcasting events
    • Applying velocity to physics object

COMMUNITY CONTENT

This tutorial was made with the help of the following Community Content:

  • SOCCER SPORT COMPONENT By PiousLachance
  • Gigantic SPORT Stuff Pack By varglbargl

Ability Phases

Abilities have four phases: Cast, Execute, Recovery, and Cooldown. This project will only focus on two of them because it will signal when the player has started kicking (Cast) and when the player has finished kicking (Recovery).

Adding an Ability

The first step is to add an ability to kick with the left mouse button.

  • From the Core Content window, drag an Ability object into the Hierarchy.
  • Open the Properties window and set the Animation property to unarmed_kick_ball.
  • Make sure the Action Binding property is set to Primary Ability.
  • Set the Cast Duration property to 0.1.
  • Set the Execute Duration property to 0.3.
  • Set the Recovery Duration property to 0.1.
  • Set the Cooldown Duration property to 0.2.

Adding an Equipment

As of now, the ability is not attached to the player so it can't be used. Equipment allows the player to pick up the ability. From the Core Content window, drag an Equipment object into the Hierarchy. Drag and drop the Ability into the Equipment so it is a child.

Testing the Ability

The Equipment has a child named BoxTrigger. If the player collides with the trigger then it will pick up the equipment. Preview the project and collide with the equipment trigger. The player should kick when clicking the left mouse button.

The KickAbility Script

Right click the Ability and create a New Script. Rename it KickAbility.

Open KickAbility Script and add the following code:

local KICKABILITY = script.parent

function ConnectAbilityEvents_KickBall(ability)
  ability.castEvent:Connect(OnCast_KickBall)
  ability.recoveryEvent:Connect(OnRecovery_KickBall)            
end

function OnCast_KickBall(ability)
    Events.Broadcast("PlayerKickStart")
end

function OnRecovery_KickBall(ability)
  Events.Broadcast("PlayerKickFinish")
end

ConnectAbilityEvents_KickBall(KICKABILITY)

The script is connecting the Cast and Recovery phase of the kick ability to functions OnCast_KickBall and OnRecovery_KickBall respectively. The functions are broadcasting an event. These events will be received in another script to control kicking the ball.

Static Player Equipment

Currently, the player must collide with the equipment trigger to activate the kick ability. Static Player Equipment allows the player to start with equipment already attached.

  • Right click the Equipment and select Create New Template From This. Rename it KickEquipment.
  • Delete the KickEquipment object from the Hierarchy.
  • From the Core Content window, drag a Static Player Equipment object into the Hierarchy.
  • Open the Properties window and set the EquipmentTemplate property to KickEquipment.

Preview the project and the player should start off with the kicking ability.

Adding a Physics Object

The soccer ball will need physics and collision logic. This makes the Physics Sphere a perfect object for this project.

  • From the Core Content window, drag a Physics Sphere object into the Hierarchy.
  • Select the Physics Sphere and open the Properties window.
  • Set the Radius property to 25.
  • Set the Angular and Linear Damping property to 0.5.
  • Delete the PhysicsSphereMesh inside the Physics Sphere's ClientContext group.
  • From the Core Content window, search for Ball - Soccer 01.
  • Drag and drop the soccer ball mesh into the Physics Sphere's ClientContext group.
  • Scale the soccer ball mesh to fit the Physics Sphere size.

Enter preview mode and check if the player can dribble with the soccer ball.

Ball Trigger

To detect if the player kicks the ball, a trigger object will need to be surrounding the ball mesh.

  • Right click the Physics Sphere and add a Trigger object.
  • Right click the Trigger and select Enable Networking.
  • Open the Properties window and change the Shape property to a sphere.
  • Transform the trigger to slightly larger than the soccer ball.

The SoccerBallKick Script

Right click the Trigger and add a new Script. Rename it SoccerBallKick. Right click the script and select Enable Networking.

Open SoccerBallKick Script and add the following code:

local TRIGGER = script.parent
local BALL = script.parent.parent

local kicked = false

local kickHeight = 0.5
local kickPower = 2300

function OnBeginOverlap(trigger, other)
    if Object.IsValid(other) and other:IsA("Player") and kicked then    
        local forwardVector = other:GetWorldTransform():GetForwardVector()
        local upwardVector = Vector3.UP * kickHeight
        local velocity = (forwardVector + upwardVector) * kickPower
        BALL:SetVelocity(velocity)
        --Make the ball spin randomly
        BALL:SetAngularVelocity(Vector3.New(math.random(-720, 720), math.random(-720, 720), math.random(-720, 720)))
        kicked = false
    end
end

TRIGGER.beginOverlapEvent:Connect(OnBeginOverlap)

function OnKickStart()
    kicked = true
end

function OnKickFinish()
    kicked = false
end

Events.Connect("PlayerKickStart", OnKickStart)
Events.Connect("PlayerKickFinish", OnKickFinish)

The PlayerKickStart and PlayerKickFinish broadcasted events from the KickAbility script are toggling the kicked boolean variable. If the player overlaps the soccer ball trigger and kicked is true, then the ball's velocity is calculated using the player's forward vector, kickHeight variable, and kickPower variable.

Play the project in Preview Mode. The player should be able to kick the ball much farther than a dribble.

Adding a Kill Zone

One current issue is the player and ball are lost in the void once they leave the Ground Floor. To fix this, a Kill Zone object can be added with some Lua scripting to respawn the player and ball.

  • From the Core Content window, drag and drop a Kill Zone object into the Hierarchy.
  • Select the Kill Trigger and open the Properties window.
  • Set the Position Z property to -300.
  • Set the Scale X and Y properties to 1000.

The player should now die when it falls off the Ground Floor.

Adding Spawn Settings

Spawn Settings will send the player back to a Spawn Point once they die.

  • From the Core Content window, drag and drop a Spawn Settings object into the Gameplay Settings group.
  • Select the Spawn Settings and open the Properties window.
  • Change the Respawn Delay property to 3.
    The player should now respawn on the Ground Floor three seconds after it dies.

The SoccerBallReset Script

Right click the Physics Sphere and add a new script. Rename it SoccerBallReset. Right click the script and select Enable Networking.

Open SoccerBallReset Script and add the following code:

local BALL = script.parent
local startingPostion = BALL:GetWorldPosition()

function Reset()
    Task.Wait(1)
    BALL:SetWorldPosition(startingPostion)
    BALL:SetVelocity(Vector3.ZERO)
    BALL:SetAngularVelocity(Vector3.ZERO)
end

Events.Connect("ResetBall", Reset)

The script is waiting for a ResetBall event to be broadcasted. The event calls the Reset function which spawns the ball at the starting position.

The KillZoneServer Script

The Kill Zone group has a child script named KillZoneServer.

Open the KillZoneServer script and replace the contents with the following code:

local KILL_TRIGGER = script:GetCustomProperty("KillTrigger"):WaitForObject()

function OnBeginOverlap(trigger, other)
    if other:IsA("Player") then
        other:Die()
    end

    local soccerBall = World.FindObjectByName("Physics Sphere")

    if soccerBall.id == other.id then
        Events.Broadcast("ResetBall")
    end
end

KILL_TRIGGER.beginOverlapEvent:Connect(OnBeginOverlap)

The script searches the Hierarchy for an object with the name Physics Sphere and broadcasts the ResetBall event if the object collides with the Kill Zone trigger.

Play the project and see if the player and ball both respawn when they collide with the Kill Zone.

Conclusion

This will be the ending point for this tutorial on making a kick ability and a kickable physics object. It's a starting off point to numerous game ideas involving soccer.

Leave a comment if you have any feedback.

Cheers!

1 Like