Skip to content
Snippets Groups Projects
MovementController.cs 1.93 KiB
Newer Older
william's avatar
william committed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovementController : MonoBehaviour
{
    private Rigidbody2D rb;
    private string horizontalAxis;
    private string verticalAxis;
    [SerializeField] private float speed;
    private Animator animatorController;
    private float timePassed;
    private bool canMove;
    private bool canShoot;

	// Use this for initialization
	void Start ()
    {
        rb = GetComponent<Rigidbody2D>();
        animatorController = GetComponent<Animator>();
        horizontalAxis = "Horizontal";
        verticalAxis = "Vertical";
        timePassed = 0;
        canMove = true;
        animatorController.SetBool("Moving", false);
        animatorController.SetBool("Shooting", false);
        animatorController.SetTrigger("AtkTurn");
    }

    private void Update()
    {
        if (rb.velocity == Vector2.zero)
        {
            animatorController.SetBool("Moving", false);
        }
        else
        {
            animatorController.SetBool("Moving", true);
        }
    }

    // Update is called once per frame
    private void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.Z))
        {
            StopCoroutine("move");
            StartCoroutine("skill");
        }
        else if (canMove)
        {
            StartCoroutine("move");
        }
    }

    private IEnumerator move()
    {
        rb.velocity = new Vector2(Input.GetAxisRaw(horizontalAxis), Input.GetAxisRaw(verticalAxis)) * speed;
        transform.rotation = rb.velocity.x <= 0 ? Quaternion.AngleAxis(180, Vector3.up) : Quaternion.AngleAxis(0, Vector3.up);
        yield return null;
    }

    private IEnumerator skill()
    {
        canMove = false;
        while (timePassed < 1)
        {
            rb.velocity = new Vector2(1, 1) * speed;
            timePassed += Time.deltaTime;
            yield return null;
        }
        timePassed = 0;
        canMove = true;
    }
}