You’ll see a way to allow a player to jump using key press
Tounderstand a way to make characters jump in a very basic way
Making objects jump in Unity can be achieved in many different ways. But the concept of jumping is fairly simple. You press a button, make sure the computer can detect the button press, make sure there is an event that happens on key press then run some code that says what I need to do.
Like most things in code for me it’s about what just happened and what does it mean. Writing this logic is easy so let’s break it down. I’m going to use my Basic Movement System for this entry to make this easier.
In My start Function I Add my physics component
This is great since I don’t need to add it into the inspector, I can just add it directly in the script, edit its properties and then I’ll always know that the component is made compared to forgetting and then wondering why I have an error in my script. I want to make gravity as true since we need a way for the player to be pulled back down to ground.
Since I’m currently using and still learning the Unity Input system, I need to now make another Input Action to add to the script. The variable public InputAction playerJump; was then made. This will allow us to set our input for jump in our inspector which I have done here.
I’ve added that the input is enable on enable and disable on disable like so
private void OnEnable()
{
playerMove.Enable();
playerJump.Enable();
}
private void OnDisable()
{
playerMove.Disable();
playerJump.Disable();
}
I also added a condition to check to see if the button was pressed.
if (playerJump.IsPressed())
isJumping = true;
else
isJumping = false;
Make sure you add yourself a debug log to check to see if it worked. Once you know it works you can then add the jump logic.
The jump logic is fairly basic and doesn’t take much real life situations into consideration. It simply lifts the object off the ground and sends it back.
Please see the script in full to get an idea on how jumping works. To make this better you could add an input counter to prevent multiple jumps happening at once by detecting a better way of knowing when the player is on the ground. You could use Collison layer detection for this.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace PlayerCharcter.Movement
{
public class Simple_3D_Move : MonoBehaviour
{
// Variables
// The Direction we are currently moving in
public Vector3 moveDirection;
public float groundPositionY;
// The Physics Component needed to move
private Rigidbody rb;
// The Speed that we move at
public float moveSpeed = 5f;
// Input System Call
public InputAction playerMove;
public InputAction playerJump;
public bool isJumping = false;
private void OnEnable()
{
playerMove.Enable();
playerJump.Enable();
}
private void OnDisable()
{
playerMove.Disable();
playerJump.Disable();
}
// Start is called before the first frame update
void Start()
{
// Make a Rigibody Physics Component in the Unity Inspector During Runtime
rb = gameObject.AddComponent<Rigidbody>();
// Turn Off the Gravity Of The Component
//rb.useGravity = false;
// Freeze all rotation
rb.freezeRotation = true;
groundPositionY = transform.position.y;
}
// Update is called once per frame
void Update()
{
// Store the Horizontal Axis In The Legacy Input System (A, D Left, Right)
//float Hor = Input.GetAxis("Horizontal");
// Store the Vertical Axis In The Legacy Input System (W, S Forward, Backward)
//float ver = Input.GetAxis("Vertical");
// Add the Input Values from the two floats to the Vector3 Axis on the X, Z axis. Leave Y Blank (Jump Axis)
//moveDirection = new Vector3(Hor, 0, ver).normalized;
moveDirection = playerMove.ReadValue<Vector3>();
// Check if Input bind is tapped
if (playerJump.IsPressed())
isJumping = true; // Start Jump code
else
isJumping = false; // Start Falling
}
// Call all Physics Updates On Fixed Update
private void FixedUpdate()
{
// Move Our Rigibody Component with it's velocity property on the Axis we declared via Input.
rb.velocity = new Vector3(moveDirection.x * moveSpeed, 0 ,moveDirection.z * moveSpeed);
// Are we Jumping?
if (isJumping)
rb.velocity = new Vector3(rb.velocity.x, 10, rb.velocity.z); // Yes
else if(!isJumping && transform.position.y > groundPositionY) // If we have increased Y position then we need to fall
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y - rb.velocity.y + (Physics.gravity.y * 15 * Time.fixedDeltaTime) , rb.velocity.z); // Falling
}
}
}