using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
Vector2 moveInput;
Rigidbody2D myrigidbody2D;
[SerializeField] float moveSpeed =10f;
[SerializeField] float jumpSpeed = 10f;
Animator myAnimator;
// Start is called before the first frame update
void Start()
{
myrigidbody2D = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
Run();
FlipSprite();
Jump();
}
void OnMove (InputValue value)
{
moveInput = value.Get<Vector2>();
}
void Run()
{
Vector2 playerVelocity = new Vector2 (moveInput.x * moveSpeed, myrigidbody2D.velocity.y);
myrigidbody2D.velocity = playerVelocity;
}
void FlipSprite()
{
bool playerHashorizontalSpeed =Mathf.Abs(myrigidbody2D.velocity.x) > Mathf.Epsilon;
if(playerHashorizontalSpeed)
{
transform.localScale = new Vector2 (Mathf.Sign(myrigidbody2D.velocity.x),1f);
myAnimator.SetBool("isRunning",true);
}
else
{
myAnimator.SetBool("isRunning",false);
}
}
void Jump(InputValue value)
{
if(value.isPressed)
{
myrigidbody2D.velocity += new Vector2(0f, jumpSpeed);
}
}
}