Pracownia-Programowania-Pro.../Assets/Scripts/PlayerController.cs
Polarjad dd529a49d1 Added project files
Added sprites for main character, idle and walking animations and scripts, gun rotation, enemies sprites and walk animation.
2020-12-03 19:38:25 +01:00

74 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("Invisible man");
SceneManager.LoadScene("GameOver");
}
void ChangeAnimationState(string newAnimation)
{
if (currentAnimaton == newAnimation) return;
animator.Play(newAnimation);
currentAnimaton = newAnimation;
}
}