1.4.2

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

using System.

Collections;
using UnityEngine;
using UnityEngine.UI;

public class PlayerMovement : MonoBehaviour


{
[SerializeField] private float speed;
[SerializeField] private float dashSpeed;
AudioManager audioManager;
private Rigidbody2D body;
private Animator anim;
private bool grounded;
private bool dash = false;
Vector2 startPos;
private Vector2 InitialPosition;
private int jumpCounter;

private void Start()


{
startPos = transform.position;
InitialPosition = transform.position;
jumpCounter = 1;
PlayStartAnimation(); // Play the start animation when the game starts
}

private void OnTriggerEnter2D(Collider2D collision)


{
if (collision.CompareTag("Damage"))
{
Die();
}
}

void Die()
{
Respawn();
}

public void Respawn()


{
transform.position = startPos;
PlayRespawnAnimation(); // Play the respawn animation
}

private void Awake()


{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
audioManager =
GameObject.FindGameObjectWithTag("Audio").GetComponent<AudioManager>();
}

private void Update()


{
float horizontalInput = Input.GetAxis("Horizontal");

if (transform.position.y <= -20f)


{
ResetPosition();
}
if (Input.GetKeyDown(KeyCode.P) && !dash)
{
StartCoroutine(Dash());
audioManager.PlaySFX(audioManager.dashsfx);
}

if (!dash)
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);

if (horizontalInput > 0.01f)


transform.localScale = Vector2.one;
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);

if (Input.GetKeyDown(KeyCode.Space) && (grounded || jumpCounter > 0))


{
if (!grounded && jumpCounter > 0)
{
jumpCounter--;
}
body.velocity = new Vector2(body.velocity.x, speed);
audioManager.PlaySFX(audioManager.jumpsfx);
}

if (Input.GetKeyDown(KeyCode.I))
{
StartCoroutine(Punch());
audioManager.PlaySFX(audioManager.punchsfx);
}
if (Input.GetKeyDown(KeyCode.O))
{
StartCoroutine(Kick());
audioManager.PlaySFX(audioManager.kicksfx);
}

anim.SetBool("run", Mathf.Abs(horizontalInput) > 0.01f);


anim.SetBool("grounded", grounded);
anim.SetBool("dash", dash);
}

private IEnumerator Dash()


{
dash = true;
float dashDuration = 0.3f;

body.velocity = new Vector2(transform.localScale.x * dashSpeed,


body.velocity.y);
anim.SetTrigger("dash");

yield return new WaitForSeconds(dashDuration);

dash = false;
}

private IEnumerator Punch()


{
anim.SetTrigger("punch");
yield return null;
}

private IEnumerator Kick()


{
anim.SetTrigger("kick");
yield return null;
}

private void OnCollisionEnter2D(Collision2D collision)


{
if (collision.gameObject.CompareTag("Ground") ||
collision.gameObject.CompareTag("Ground1"))
{
grounded = true;
jumpCounter = 1;
}
}

private void OnCollisionExit2D(Collision2D collision)


{
if (collision.gameObject.CompareTag("Ground") ||
collision.gameObject.CompareTag("Ground1"))
{
grounded = false;
}
}

void ResetPosition()
{
transform.position = InitialPosition;
body.velocity = Vector2.zero;
}

void PlayStartAnimation()
{
anim.SetTrigger("Start");
}

void PlayRespawnAnimation()
{
anim.SetTrigger("Start");
}
}

You might also like