Scriptum/Assets/Scripts/Player.cs

134 lines
3.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody2D rb;
private Animator myAnimator;
public float walkSpeed = 4f;
float speedLimiter = 0.7f;
float inputHorizontal;
float inputVertical;
public FloatValue maxHealth;
public float health;
private bool attack;
public GameObject Panel;
private bool inRange = false;
public ParticleSystem dmgParticleSystem;
private bool canWalk = true;
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
health = maxHealth.initialValue;
walkSpeed = 4f;
}
public void TakeDamage(float damage)
{
health = health - damage;
var em = dmgParticleSystem.emission;
em.enabled = true;
StartCoroutine(Timer());
if (health <= 0)
{
Panel.SetActive(true);
walkSpeed = 0f;
}
}
IEnumerator Timer()
{
yield return new WaitForSeconds(0.2f);
var em = dmgParticleSystem.emission;
em.enabled = false;
}
private void HandleAttacks()
{
if (attack)
{
myAnimator.SetTrigger("attack");
}
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Space))
{
attack = true;
}
}
private void ResetValues()
{
attack = false;
}
void Update()
{
//if (canWalk == true)
//{
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
myAnimator.SetFloat("moveX", inputHorizontal * walkSpeed);
myAnimator.SetFloat("moveY", inputVertical * walkSpeed);
if (inputHorizontal != 0)
{
myAnimator.SetFloat("speed", walkSpeed);
}
else if (inputVertical != 0)
{
myAnimator.SetFloat("speed", walkSpeed);
}
else
{
myAnimator.SetFloat("speed", 0);
}
if (inputHorizontal == 1 || inputHorizontal == -1 || inputVertical == 1 || inputVertical == -1)
{
myAnimator.SetFloat("lastMoveX", inputHorizontal);
myAnimator.SetFloat("lastMoveY", inputVertical);
}
//}
HandleInput();
}
void FixedUpdate()
{
if(canWalk == true)
{
if (inputHorizontal != 0 || inputVertical != 0)
{
if (inputHorizontal != 0 && inputVertical != 0)
{
inputHorizontal *= speedLimiter;
inputVertical *= speedLimiter;
}
rb.velocity = new Vector2(inputHorizontal * walkSpeed, inputVertical * walkSpeed);
}
else
{
rb.velocity = new Vector2(0f, 0f);
}
}
HandleAttacks();
ResetValues();
}
}