📜  unity 2d jump - C# (1)

📅  最后修改于: 2023-12-03 15:20:51.498000             🧑  作者: Mango

Unity 2D Jump - C#

Introduction

In this tutorial, we will be creating a simple 2D jump mechanic in Unity using C#. This tutorial assumes basic knowledge of Unity, C# and 2D game design.

Prerequisites
  • Unity 2019.4.18f1 or later
  • Basic knowledge of Unity
  • Basic knowledge of C#
Steps
Step 1: Create a New Unity Project
  • Open Unity and create a new 2D project.
Step 2: Create a Player GameObject
  • Create a new GameObject by right-clicking in the Hierarchy panel and selecting Create Empty.
  • Rename this new GameObject to something like Player.
  • Add a SpriteRenderer component to the Player object by clicking the Add Component button in the Inspector panel and typing SpriteRenderer in the search bar. Select this component once it appears.
  • Add a Rigidbody2D component to the Player object by clicking the Add Component button in the Inspector panel and typing Rigidbody2D in the search bar. Select this component once it appears.
Step 3: Create a Ground GameObject
  • Create a new GameObject by right-clicking in the Hierarchy panel and selecting Create Empty.
  • Rename this new GameObject to something like Ground.
  • Add a SpriteRenderer component to this GameObject.
  • Add a BoxCollider2D component to this GameObject.
Step 4: Script the Player's Jump
  • Create a new C# script by right-clicking in the Project panel and selecting Create/C# Script.
  • Name the script something like PlayerJump.
  • Open the script in your preferred code editor.
  • Add the following code to the PlayerJump script:
using UnityEngine;

public class PlayerJump : MonoBehaviour
{
    [SerializeField] private float jumpForce = 5f;
    [SerializeField] private LayerMask groundLayer;
    [SerializeField] private Transform groundCheck;

    private Rigidbody2D rb;
    private bool isGrounded = false;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate()
    {
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, groundLayer);
    }

    private void Update()
    {
        if (isGrounded && Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = Vector2.up * jumpForce;
        }
    }
}
Step 5: Attach the Script to the Player GameObject
  • Drag the PlayerJump script from the Project panel and drop it onto the Player object in the Hierarchy panel.
Step 6: Configure the Ground GameObject
  • Select the Ground object in the Hierarchy panel.
  • In the Inspector panel, check the Is Trigger checkbox on the BoxCollider2D component and set the size to the size of your ground sprite.
Step 7: Configure the Ground Layer
  • In the Project panel, click on the Layers dropdown menu and select Edit Layers....
  • Add a new layer called Ground.
  • Select the Ground object in the Hierarchy panel.
  • In the Inspector panel, change the Layer dropdown to Ground.
Step 8: Configure the Ground Check
  • Select the Player object in the Hierarchy panel.
  • In the Inspector panel, drag the Ground object from the Hierarchy panel to the Ground Check field on the PlayerJump component.
Step 9: Test the Game
  • Press Play in Unity and test your game by jumping on and off the ground.
Conclusion

In this tutorial, we created a simple 2D jump mechanic in Unity using C#. By following these steps, you should now have a basic understanding of how to script player movement in Unity.