Восстановление здоровья и временное бессмертие
Добавлено: 13 янв 2020, 14:40
Доброго времени суток. После того, как я добавил в код все необходимое для работы гема неуязвимости, у меня перестали работать сердца. Теперь, когда я их подбираю, здоровье не восстанавливается и смерть персонажа становится невозможной. Я даже проверил — я вернул строчку curHp = curHp + deltaHp; на свое прежнее место в public void RecountHp(int deltaHp) перед первым if, а те две новые, которые были добавлены в if (deltaHp < 0 && canHit) и в else if (curHp > maxHp) я удалил, как это было до введения гема бессмертия, и после этого сердца снова стали работать как должны. Вот весь мой код, помогите, пожалуйста, как сделать так, чтобы работали и сердца и гем неуязвимости:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody2D rb;
public float speed;
public float jumpHeight;
public Transform groundCheck;
bool isGrounded;
Animator anim;
int curHp;
int maxHp = 3;
bool isHit = false;
public Main main;
bool canHit = true;
public bool key = false;
bool canTP = true;
public bool inWater = false;
bool isClimbing = false;
int coins = 0;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
curHp = maxHp;
}
// Update is called once per frame
void Update()
{
if (inWater && !isClimbing)
{
anim.SetInteger("State", 4);
isGrounded = true;
if (Input.GetAxis("Horizontal") != 0)
Flip();
}
else
{
CheckGround();
if (Input.GetAxis("Horizontal") == 0 && (isGrounded) && !isClimbing)
{
anim.SetInteger("State", 1);
}
else
{
Flip();
if (isGrounded && !isClimbing)
anim.SetInteger("State", 2);
}
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
rb.AddForce(transform.up * jumpHeight, ForceMode2D.Impulse);
}
void Flip()
{
if (Input.GetAxis("Horizontal") > 0)
transform.localRotation = Quaternion.Euler(0, 0, 0);
if (Input.GetAxis("Horizontal") < 0)
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 0.2f);
isGrounded = colliders.Length > 1;
if (!isGrounded && !isClimbing)
anim.SetInteger("State", 3);
}
public void RecountHp(int deltaHp)
{
if (deltaHp < 0 && canHit)
{
curHp = curHp + deltaHp;
StopCoroutine(OnHit());
canHit = false;
isHit = true;
StartCoroutine(OnHit());
}
else if (curHp > maxHp)
{
curHp = curHp + deltaHp;
curHp = maxHp;
}
print(curHp);
if (curHp <= 0)
{
GetComponent<CapsuleCollider2D>().enabled = false;
Invoke("Lose", 2f);
}
}
IEnumerator OnHit()
{
if (isHit)
GetComponent<SpriteRenderer>().color = new Color(1f, GetComponent<SpriteRenderer>().color.g - 0.08f, GetComponent<SpriteRenderer>().color.b - 0.08f);
else
GetComponent<SpriteRenderer>().color = new Color(1f, GetComponent<SpriteRenderer>().color.g + 0.08f, GetComponent<SpriteRenderer>().color.b + 0.08f);
if (GetComponent<SpriteRenderer>().color.g == 1f)
{
StopCoroutine(OnHit());
canHit = true;
}
if (GetComponent<SpriteRenderer>().color.g <= 0)
isHit = false;
yield return new WaitForSeconds(0.04f);
StartCoroutine(OnHit());
}
void Lose()
{
main.GetComponent<Main>().Lose();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "key")
{
Destroy(collision.gameObject);
key = true;
}
if (collision.gameObject.tag == "Door")
{
if (collision.gameObject.GetComponent<Door>().isOpen && canTP)
{
collision.gameObject.GetComponent<Door>().Teleport(gameObject);
canTP = false;
StartCoroutine(TPwait());
}
else if (key)
collision.gameObject.GetComponent<Door>().Unlock();
}
if (collision.gameObject.tag == "coin")
{
Destroy(collision.gameObject);
coins++;
print("Кол-во монеток равно " + coins);
}
if (collision.gameObject.tag == "Heart")
{
Destroy(collision.gameObject);
RecountHp(1);
}
if (collision.gameObject.tag == "mushroom")
{
Destroy(collision.gameObject);
RecountHp(-1);
}
if (collision.gameObject.tag == "Bonus1")
{
Destroy(collision.gameObject);
StartCoroutine(NoHit());
}
}
IEnumerator TPwait()
{
yield return new WaitForSeconds(1f);
canTP = true;
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.tag == "Ladder")
{
isClimbing = true;
rb.bodyType = RigidbodyType2D.Kinematic;
if (Input.GetAxis("Vertical") == 0)
{
anim.SetInteger("State", 5);
}
else
{
anim.SetInteger("State", 6);
transform.Translate(Vector3.up * Input.GetAxis("Vertical") * speed * 0.5f * Time.deltaTime);
}
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Ladder")
{
isClimbing = false;
rb.bodyType = RigidbodyType2D.Dynamic;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Trampoline")
StartCoroutine(TrampolineAnim(collision.gameObject.GetComponentInParent<Animator>()));
}
IEnumerator TrampolineAnim(Animator an)
{
an.SetBool("isJump", true);
yield return new WaitForSeconds(0.5f);
an.SetBool("isJump", false);
}
IEnumerator NoHit()
{
canHit = false;
print("Неуязвимость активирована");
yield return new WaitForSeconds(11f);
canHit = true;
print("Неуязвимость закончилась");
}
}
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody2D rb;
public float speed;
public float jumpHeight;
public Transform groundCheck;
bool isGrounded;
Animator anim;
int curHp;
int maxHp = 3;
bool isHit = false;
public Main main;
bool canHit = true;
public bool key = false;
bool canTP = true;
public bool inWater = false;
bool isClimbing = false;
int coins = 0;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
curHp = maxHp;
}
// Update is called once per frame
void Update()
{
if (inWater && !isClimbing)
{
anim.SetInteger("State", 4);
isGrounded = true;
if (Input.GetAxis("Horizontal") != 0)
Flip();
}
else
{
CheckGround();
if (Input.GetAxis("Horizontal") == 0 && (isGrounded) && !isClimbing)
{
anim.SetInteger("State", 1);
}
else
{
Flip();
if (isGrounded && !isClimbing)
anim.SetInteger("State", 2);
}
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
rb.AddForce(transform.up * jumpHeight, ForceMode2D.Impulse);
}
void Flip()
{
if (Input.GetAxis("Horizontal") > 0)
transform.localRotation = Quaternion.Euler(0, 0, 0);
if (Input.GetAxis("Horizontal") < 0)
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 0.2f);
isGrounded = colliders.Length > 1;
if (!isGrounded && !isClimbing)
anim.SetInteger("State", 3);
}
public void RecountHp(int deltaHp)
{
if (deltaHp < 0 && canHit)
{
curHp = curHp + deltaHp;
StopCoroutine(OnHit());
canHit = false;
isHit = true;
StartCoroutine(OnHit());
}
else if (curHp > maxHp)
{
curHp = curHp + deltaHp;
curHp = maxHp;
}
print(curHp);
if (curHp <= 0)
{
GetComponent<CapsuleCollider2D>().enabled = false;
Invoke("Lose", 2f);
}
}
IEnumerator OnHit()
{
if (isHit)
GetComponent<SpriteRenderer>().color = new Color(1f, GetComponent<SpriteRenderer>().color.g - 0.08f, GetComponent<SpriteRenderer>().color.b - 0.08f);
else
GetComponent<SpriteRenderer>().color = new Color(1f, GetComponent<SpriteRenderer>().color.g + 0.08f, GetComponent<SpriteRenderer>().color.b + 0.08f);
if (GetComponent<SpriteRenderer>().color.g == 1f)
{
StopCoroutine(OnHit());
canHit = true;
}
if (GetComponent<SpriteRenderer>().color.g <= 0)
isHit = false;
yield return new WaitForSeconds(0.04f);
StartCoroutine(OnHit());
}
void Lose()
{
main.GetComponent<Main>().Lose();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "key")
{
Destroy(collision.gameObject);
key = true;
}
if (collision.gameObject.tag == "Door")
{
if (collision.gameObject.GetComponent<Door>().isOpen && canTP)
{
collision.gameObject.GetComponent<Door>().Teleport(gameObject);
canTP = false;
StartCoroutine(TPwait());
}
else if (key)
collision.gameObject.GetComponent<Door>().Unlock();
}
if (collision.gameObject.tag == "coin")
{
Destroy(collision.gameObject);
coins++;
print("Кол-во монеток равно " + coins);
}
if (collision.gameObject.tag == "Heart")
{
Destroy(collision.gameObject);
RecountHp(1);
}
if (collision.gameObject.tag == "mushroom")
{
Destroy(collision.gameObject);
RecountHp(-1);
}
if (collision.gameObject.tag == "Bonus1")
{
Destroy(collision.gameObject);
StartCoroutine(NoHit());
}
}
IEnumerator TPwait()
{
yield return new WaitForSeconds(1f);
canTP = true;
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.gameObject.tag == "Ladder")
{
isClimbing = true;
rb.bodyType = RigidbodyType2D.Kinematic;
if (Input.GetAxis("Vertical") == 0)
{
anim.SetInteger("State", 5);
}
else
{
anim.SetInteger("State", 6);
transform.Translate(Vector3.up * Input.GetAxis("Vertical") * speed * 0.5f * Time.deltaTime);
}
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Ladder")
{
isClimbing = false;
rb.bodyType = RigidbodyType2D.Dynamic;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Trampoline")
StartCoroutine(TrampolineAnim(collision.gameObject.GetComponentInParent<Animator>()));
}
IEnumerator TrampolineAnim(Animator an)
{
an.SetBool("isJump", true);
yield return new WaitForSeconds(0.5f);
an.SetBool("isJump", false);
}
IEnumerator NoHit()
{
canHit = false;
print("Неуязвимость активирована");
yield return new WaitForSeconds(11f);
canHit = true;
print("Неуязвимость закончилась");
}
}