We will now add a code to make the ball jump.
In the Assets folder, right click > Create > Folder, name it "Scripts"
Right click in the folder > Create > C# Script
Name the script "Ball", click on the script to open it in Visual Studio
Copy the following code into the script and save the file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
/*
Variable -------------------------------->Data Type-------------------------> Usage
JumpKeywaspressed-------------->Boolean-----------------------------> Changed to true when the space bar is pressed
space_press_count ---------------->Integer------------------------------> Tracks of the number of jumps(max of 3 without touching the platform)
rigidbody_component-------------->Rigidbody--------------------------> Prevent calling the same class/component repeatedly on Update function
ground_check------------------------>Transform--------------------------> Store the transform component of the gameobject that is used to check if the ball is touching the ground
SpawnPoint-------------------------->Transform-------------------------->Store the transform component of the gameobject that is used to respawn the ball
level1---------------------------------->GameObject------------------------>Store the "Level 1" gameobject, which is used to identify if the ball has fallen off
*/
//Private variable allow the use of this variable on this class ONLY
//[SerializeField] makes the variable display on the inspector window of unity
private bool jumpKeywaspressed = false;
private int space_press_count = 0;
private float horizontal_Input;
private Rigidbody rigidbody_component;
[SerializeField] private Transform ground_check;
[SerializeField] private Transform SpawnPoint;
public GameObject level1;
// Start is called before the first frame update
void Start()
{
rigidbody_component = GetComponent<Rigidbody>();
level1 = GameObject.Find("Level 1");//In this way the Level 1 doesn't need to be drag into the inspector, the code will find it from the scene
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) //Check if the space bar was pressed
{
jumpKeywaspressed = true;
}
horizontal_Input = Input.GetAxis("Horizontal"); //Store input from right-left arrow keys, "A" and "D"
// Moves the ball to the respawn location if the ball's position is 3 unit below the Level 1's position
if (GetComponent<Transform>().position.y < (level1.GetComponent<Transform>().position.y - 3.0f)) // "f" indicate the number is a floating type
{
GetComponent<Transform>().position = SpawnPoint.transform.position;
}
}
private void FixedUpdate()
{
//Assinging horizontal velocity to the ball by making horizontal input multiply by 3
rigidbody_component.velocity = new Vector3(3 * horizontal_Input, rigidbody_component.velocity.y, 0);
/* The following if statement checks if the ground_check gameobject at bottom of the ball is colliding with the ground.
Physics.OverlapSphere create an imaginary sphere around that gound_check position with the diameter of 0.1 unit and output an array of information of the collision that the imaginary sphere is having. The array length of 1 will indicate the imaginary sphere is colliding with the ball only*/
if (Physics.OverlapSphere(ground_check.position, 0.1f).Length == 1)
{
if (jumpKeywaspressed && space_press_count < 2)
{
rigidbody_component.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
jumpKeywaspressed = false;
space_press_count = space_press_count + 1;
}
}
else
{
space_press_count = 0;
if (jumpKeywaspressed)
{
rigidbody_component.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
jumpKeywaspressed = false;
space_press_count = space_press_count + 1;
}
}
}
}
Click on the Ball in the Hierarchy window
Click on the Scripts folder and drag the Ball script into the inspector window of the Ball
- From the Hierarchy window drag the GroundCheck component to the Ground_Check placeholder
- From the Hierarchy window drag the Spawnpoint component to the Spawnpoint placeholder
- From the Hierarchy window drag the Level 1 component to the Level 1 placeholder