using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerController : Entity
{
public float speed;
public float jumpForce;
private float moveInput;
[SerializeField] private int lives = 3;
private int health;
[SerializeField] private Image[] hearts;
[SerializeField] private Sprite aliveHeart;
[SerializeField] private Sprite deadHeart;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
private Animator anim;
public static PlayerController Instance { get; set; }
private void Start()
{
anim = GetComponent();
rb = GetComponent();
}
private void Awake()
{
Instance = this;
lives = 3;
health = lives;
}
private void FixedUpdate()
{
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if (facingRight == false && moveInput 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{
Flip();
}
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
}
private void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = Vector2.up * jumpForce;
anim.SetTrigger("takeOf");
}
if (isGrounded == true)
{
anim.SetBool("isJumping", false);
}
else
{
anim.SetBool("isJumping", true);
}
if (health lives)
health = lives;
for (int i = 0; i < hearts.Length; i++)
{
if (i < health)
hearts.sprite = aliveHeart;
else
hearts.sprite = deadHeart;
if (i < lives)
hearts.enabled = true;
else
hearts.enabled = false;
}
if (lives < 1)
Die();
}
void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
else if (moveInput 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
}
public override void GetDamage()
{
lives -= 1;
Debug.Log(lives);
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... platformer
Как заставить противника оттолкнуть персонажа в моем платформере? ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение