93 lines
2.2 KiB
C#
93 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Player : MonoBehaviour
|
|
{
|
|
|
|
Rigidbody2D rb;
|
|
private Animator myAnimator;
|
|
float walkSpeed = 4f;
|
|
float speedLimiter = 0.7f;
|
|
float inputHorizontal;
|
|
float inputVertical;
|
|
public FloatValue currentHealth;
|
|
private bool attack;
|
|
void Start()
|
|
{
|
|
rb = gameObject.GetComponent<Rigidbody2D>();
|
|
myAnimator = GetComponent<Animator>();
|
|
}
|
|
|
|
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()
|
|
{
|
|
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(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();
|
|
}
|
|
|
|
}
|