
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
public class Character : MonoBehaviour
{
public Rigidbody2D playerRigitboody;
public Animator charAnimator;
public SpriteRenderer sprite;
public int lives = 5;
public float speed = 4.0f;
public float jumpForce;
// Проверяет находится ли персонаж на земле.
private bool isGrounded;
// Проверяет есть ли под персонажем земля.
public Transform groundCheck;
// На каком расстояние от земли возможно совершить двойной прыжок.
public float checkRadidus;
// Указывает на то, что является землёй.
public LayerMask whatIsGround;
// Указывает количество прыжков.
private int extraJumps;
public int extraJumpsValue;
private void FixedUpdate()
{
// создание окружности под ногами персонажа для проверки находиться ли там земля
isGrounded = Physics2D.OverlapCircle(groundCheck.position,checkRadidus, whatIsGround);
}
private void Awake()
{
playerRigitboody = GetComponentInChildren<Rigidbody2D>();
charAnimator = GetComponentInChildren<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
}
void Start()
{
extraJumps = extraJumpsValue;
}
/// <summary>
/// Движение персонажа.
/// </summary>
void Move()
{
Vector3 tempVector = Vector3.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + tempVector, speed * Time.deltaTime);
// Изменяем от рисовку анимации в зависимости от направления.
if (tempVector.x < 0)
{
sprite.flipX = true;
}
else
{
sprite.flipX = false;
}
}
/// <summary>
/// Прыжок персонажа.
/// </summary>
void Jump()
{
//charAnimator.SetInteger("actCharacer", 4);
charAnimator.PlayInFixedTime("actCharacer", 4);
playerRigitboody.velocity = Vector2.up * jumpForce;
// Ограничивает количество прыжков.
extraJumps--;
// TODO: Анимация.
}
/// <summary>
/// Взбираться по лестнице
/// </summary>
public void Climb()
{
Vector3 tempVector = Vector3.up * Input.GetAxis("Vertical");
transform.position = Vector3.MoveTowards(transform.position, transform.position + tempVector, speed * Time.deltaTime);
}
// Update is called once per frame
void Update()
{
if (isGrounded == true)
{
extraJumps = extraJumpsValue;
}
if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
{
charAnimator.CrossFade("actCharacer", 4);
Jump();
}
else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
{
playerRigitboody.velocity = Vector2.up * jumpForce;
}
if (Input.GetButton("Horizontal"))
{
Move();
charAnimator.SetInteger("actCharacer", 1);
}
else
{
charAnimator.SetInteger("actCharacer", 0);
}
if (Input.GetButton("Vertical"))
{
Climb();
charAnimator.SetInteger("actCharacer", 2);
}
#region Атака
//}
//else if (Input.GetButton("Fire1"))
//{
// Debug.Log("Z");
// sprite.flipX = true;
// charAnimator.SetInteger("actCharacer", 3);
//}
//else if (Input.GetButton("Fire2"))
//{
// Debug.Log("X");
// sprite.flipX = false;
// charAnimator.SetInteger("actCharacer", 3);
//}
#endregion
//else
//{
// // Вызываем анимацию 0 (Idle).
//charAnimator.SetInteger("actCharacer", 0);
//}
#region Атака
///// <summary>
///// Выкапывает платформу с права.
///// </summary>
//void AttackRight()
//{
//}
///// <summary>
///// Выкапывает платформу с лева.
///// </summary>
//void AttackLeft()
//{
//}
#endregion
}
}