2020-12-03 19:38:25 +01:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
2021-01-25 22:40:51 +01:00
|
|
|
|
|
2020-12-03 19:38:25 +01:00
|
|
|
|
|
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public float movementSpeed = 5f;
|
|
|
|
|
public Rigidbody2D rb;
|
|
|
|
|
Vector2 movement;
|
|
|
|
|
private Animator animator;
|
|
|
|
|
private bool FacingRight = true;
|
|
|
|
|
|
2021-01-13 18:58:14 +01:00
|
|
|
|
|
2020-12-03 19:38:25 +01:00
|
|
|
|
private string currentAnimaton;
|
|
|
|
|
const string PLAYER_IDLE = "Player_Idle";
|
|
|
|
|
const string PLAYER_WALK = "Player_Walk";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
animator = GetComponent<Animator>();
|
2021-01-13 18:58:14 +01:00
|
|
|
|
|
2020-12-03 19:38:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
|
|
|
|
movement.x = Input.GetAxisRaw("Horizontal");
|
|
|
|
|
movement.y = Input.GetAxisRaw("Vertical");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FixedUpdate()
|
|
|
|
|
{
|
|
|
|
|
rb.MovePosition(rb.position + movement * movementSpeed * Time.fixedDeltaTime);
|
|
|
|
|
if (movement.x < 0 && FacingRight)
|
|
|
|
|
{
|
|
|
|
|
Flip();
|
|
|
|
|
}
|
|
|
|
|
else if (movement.x > 0 && !FacingRight)
|
|
|
|
|
{
|
|
|
|
|
Flip();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((movement.x != 0) || (movement.y != 0))
|
|
|
|
|
{
|
|
|
|
|
ChangeAnimationState(PLAYER_WALK);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ChangeAnimationState(PLAYER_IDLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Flip()
|
|
|
|
|
{
|
|
|
|
|
FacingRight = !FacingRight;
|
|
|
|
|
|
|
|
|
|
transform.Rotate(0f, 180f, 0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ChangeAnimationState(string newAnimation)
|
|
|
|
|
{
|
|
|
|
|
if (currentAnimaton == newAnimation) return;
|
|
|
|
|
|
|
|
|
|
animator.Play(newAnimation);
|
|
|
|
|
currentAnimaton = newAnimation;
|
|
|
|
|
}
|
2021-01-13 18:58:14 +01:00
|
|
|
|
|
2020-12-03 19:38:25 +01:00
|
|
|
|
}
|