Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
863 B
1
Indexable
Never
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{ 
  public float moveSpeed;
    public float jumpForce;
  public bool isJumping = false;
  public Rigidbody2D rb;
  private Vector3 velocity=Vector3.zero;    

    void Update()
    {
        float horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
     if(Input.GetButtonDown("Jump"))
     {
            isJumping = true;
     }

     MovePlayer(horizontalMovement);
    }
 
     void MovePlayer(float _horizontalMovement)   
     {
     Vector3 targetVelocity = new Vector2(_horizontalMovement, rb.velocity.y);
     rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref velocity, .05f);

     if(isJumping == true)
     {
      rb.AddForce(new Vector2(0f, jumpForce));
            isJumping = false;
     }

     }
 
}    
Leave a Comment