unity 2d c# how to make a simple character platformer movement (has jumping phychics)
Creating a simple character platformer movement in Unity 2D involves handling input for moving left and right, jumping, and applying some physics. Below is a step-by-step guide to help you set up a basic 2D platformer movement with jumping physics.
### Step 1: Set Up the Project
1. **Create a New 2D Project**:
- Open Unity, create a new project, and select the 2D template.
2. **Add a 2D Character**:
- Create a new GameObject for the player. You can use a simple sprite or create a new sprite for the character.
- Add a `SpriteRenderer` component to your player GameObject and assign a sprite.
- Optionally, you can add a `BoxCollider2D` to your player for collision detection.
3. **Add a Ground Object**:
- Create a ground GameObject in your scene to act as the platform. Add a `BoxCollider2D` to this object too, so your character can stand on it.
### Step 2: Create the Player Movement Script
1. **Create a Script**:
- Right-click in the `Assets` folder, go to **Create > C# Script**, and name it `PlayerMovement`.
2. **Open the Script**:
- Double-click the `PlayerMovement` script to open it in your preferred code editor.
3. **Implement the Movement Logic**:
- Replace the contents of the `PlayerMovement.cs` with the following code:
```csharp
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f; // Speed of the player
public float jumpForce = 10f; // Force applied when jumping
private Rigidbody2D rb; // Reference to the Rigidbody2D component
private bool isGrounded; // To check if the player is on the ground
private void Start()
{
rb = GetComponent<Rigidbody2D>(); // Get the Rigidbody2D component
}
private void Update()
{
Move();
Jump();
}
private void Move()
{
float moveInput = Input.GetAxis("Horizontal"); // Get the horizontal input (A/D, Left/Right)
Vector2 moveVelocity = new Vector2(moveInput * moveSpeed, rb.velocity.y); // Create a velocity vector
rb.velocity = moveVelocity; // Assign the velocity to the Rigidbody2D
}
private void Jump()
{
if (isGrounded && Input.GetButtonDown("Jump")) // Check if the player is grounded and the jump button is pressed
{
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse); // Apply the jump force
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground")) // Check if the collision object has the tag "Ground"
{
isGrounded = true; // Set isGrounded to true
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground")) // Check if the collision object has the tag "Ground"
{
isGrounded = false; // Set isGrounded to false
}
}
}
```
### Step 3: Attach the Script to the Player
1. Select your player GameObject in the hierarchy.
2. Drag and drop the `PlayerMovement` script from the `Assets` into the player GameObject in the inspector.
### Step 4: Set Up Physics
1. **Add a Rigidbody2D**:
- Select your player GameObject and add a `Rigidbody2D` component to it. Set the following properties:
- **Gravity Scale**: Typically set to `1` (default).
- **Mass**: This can be adjusted, but 1 is usually fine for simple games.
2. **Tag the Ground**:
- Select your ground GameObject and in the Inspector, add a tag named “Ground”. If it doesn’t exist, create a new tag by clicking “Add Tag…” then create the “Ground” tag.
### Step 5: Configure Input
1. By default, Unity uses "Horizontal" (for left/right movement) and "Jump" (for jumping with the spacebar).
- You can customize input settings in `Edit > Project Settings > Input Manager`.
### Step 6: Test Your Game
1. Save your scene (File > Save).
2. Press the Play button in Unity to test your character movement:
- Use the left and right arrow keys or "A" and "D" to move.
- Use the spacebar to jump.
### Additional Notes
- **Smooth Jumping**: You might want to add more advanced jumping physics (like implementing variable jump height). For simple implementations, gravity scale and jump force should suffice.
- **Animations**: For a more polished feel, consider adding animations for running and jumping.
- You can enhance the physics further by refining the Rigidbody2D settings or implementing additional features like double jumps, sliding, or wall jumps.
This is a basic setup. Once you get this working, you can start adding more features like animations, sound effects, or even enemies!