Untitled
unknown
plain_text
a year ago
1.2 kB
3
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float jumpHeight = 7f;
    private Rigidbody2D rb;
    private bool isGrounded;
    void Start()
    {
        // Rigidbody2D component initialize kora
        rb = GetComponent<Rigidbody2D>();
    }
    void Update()
    {
        // Player movement er code (left-right)
        float moveInput = Input.GetAxis("Horizontal"); // Arrow keys or A/D
        rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
        // Player jump (up)
        if (isGrounded && Input.GetKeyDown(KeyCode.Space)) // Space bar to jump
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
        }
    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        // Check korche player ground e ache kina
        if (collision.collider.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }
    void OnCollisionExit2D(Collision2D collision)
    {
        // Player ground theke ber hole, isGrounded false
        if (collision.collider.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
}Editor is loading...
Leave a Comment