Pracownia-Programowania-Pro.../Assets/Scripts/PlayerController.cs
Polarjad 97b7b765fd Added enemy AI
Added wip sprites for map, changed shooting, added 2 different enemy AI, added health system
2021-01-13 18:58:14 +01:00

77 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
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 OnBecameInvisible()
{
Debug.Log("You died");
SceneManager.LoadScene("GameOver");
}
void ChangeAnimationState(string newAnimation)
{
if (currentAnimaton == newAnimation) return;
animator.Play(newAnimation);
currentAnimaton = newAnimation;
}
}