Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.2 kB
3
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float playerSpeed;
    [SerializeField] float playerJump;
    private float playerMoveDir;
    private bool bJumping = false;
    [SerializeField] Rigidbody2D playerRB;
    // Start is called before the first frame update

    // Update is called once per frame
    void Update()
    {
        playerMoveDir = Input.GetAxis("Horizontal");

        playerRB.velocity = new Vector2(playerSpeed * playerMoveDir, playerRB.velocity.y);

        if(Input.GetButtonDown("Jump") && (bJumping == false))
        {
            playerRB.AddForce(new Vector2(playerRB.velocity.x, playerJump));
        }
    }

    private void OnCollisionEnter2D(Collision2D objCol)
    {        
        if (objCol.gameObject.CompareTag("Floor"))
        {
            bJumping = false;
        }

        if (objCol.gameObject.CompareTag("Enemy"))
        {
            Destroy(gameObject);
        }
    }

    private void OnCollisionExit(Collision objCol)
    {
        if (objCol.gameObject.CompareTag("Floor"))
        {
            bJumping = true;
        }
    }
}
Leave a Comment