Pracownia-Programowania-Pro.../Assets/Scripts/Player scripts/PlayerController.cs
Polarjad 1ea616dd60 Added dungeon textures, added scenes, added UI
Enemy balance, added upgrades, added score, added menu
2021-01-25 22:40:51 +01:00

70 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movementSpeed = 5f;
public Rigidbody2D rb;
Vector2 movement;
private Animator animator;
private bool FacingRight = true;
private string currentAnimaton;
const string PLAYER_IDLE = "Player_Idle";
const string PLAYER_WALK = "Player_Walk";
void Start()
{
animator = GetComponent<Animator>();
}
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;
}
}