Scriptum/Assets/Scripts/Player.cs

67 lines
1.8 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2022-04-07 23:46:09 +02:00
public class Player : MonoBehaviour
{
Rigidbody2D rb;
2022-04-07 23:46:09 +02:00
private Animator myAnimator;
float walkSpeed = 4f;
float speedLimiter = 0.7f;
float inputHorizontal;
float inputVertical;
2022-05-18 13:58:41 +02:00
public FloatValue currentHealth;
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
2022-04-07 23:46:09 +02:00
myAnimator = GetComponent<Animator>();
}
void Update()
{
inputHorizontal = Input.GetAxisRaw("Horizontal");
inputVertical = Input.GetAxisRaw("Vertical");
2022-04-07 23:46:09 +02:00
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);
}
}
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);
}
}
}