2022-03-24 18:37:17 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
2022-04-07 23:46:09 +02:00
|
|
|
public class Player : MonoBehaviour
|
2022-03-24 18:37:17 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
Rigidbody2D rb;
|
2022-04-07 23:46:09 +02:00
|
|
|
private Animator myAnimator;
|
2022-03-24 18:37:17 +01:00
|
|
|
float walkSpeed = 4f;
|
|
|
|
float speedLimiter = 0.7f;
|
|
|
|
float inputHorizontal;
|
|
|
|
float inputVertical;
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
rb = gameObject.GetComponent<Rigidbody2D>();
|
2022-04-07 23:46:09 +02:00
|
|
|
myAnimator = GetComponent<Animator>();
|
2022-03-24 18:37:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2022-03-24 18:37:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|