Код скрипта, который я креплю к плееру:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Plsyer : MonoBehaviour
{
Rigidbody2D rb;
public float speed;
public float jumpHeight;
public Transform groundCheck;
bool isGrounded;
Animator anim;
int curHp;
int maxHp = 3;
bool isHit = false;
// 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()
{
CheckGround();
if (Input.GetAxis("Horizontal") == 0 && (isGrounded))
{
anim.SetInteger("State", 1);
}
else
{
Flip();
if (isGrounded)
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(groundCh eck.position, 0.2f);
isGrounded = colliders.Length > 1;
if (!isGrounded)
anim.SetInteger("State", 3);
}
public void RecountHp(int deltaHp)
{
curHp = curHp + deltaHp;
if (deltaHp < 0)
{
StopCoroutine(OnHit());
isHit = true;
StartCoroutine(OnHit());
}
print(curHp);
if (curHp <= 0)
{
GetComponent<CapsuleCollider2D>().e nabled = false;
}
}
IEnumerator OnHit()
{
if (isHit)
GetComponent<SpriteRenderer>().colo r = new Color(1f, GetComponent<SpriteRenderer>().colo r.g - 0.04f, GetComponent<SpriteRenderer>().colo r.b - 0.04f);
else
GetComponent<SpriteRenderer>().colo r = new Color(1f, GetComponent<SpriteRenderer>().colo r.g + 0.04f, GetComponent<SpriteRenderer>().colo r.b - 0.04f);
if (GetComponent<SpriteRenderer>().col or.g <= 1)
StopCoroutine(OnHit());
if (GetComponent<SpriteRenderer>().col or.g <= 0)
isHit = false;
yield return new WaitForSeconds(0.02f);
StartCoroutine(OnHit());
}
}
using System.Collections.Generic;
using UnityEngine;
public class Plsyer : MonoBehaviour
{
Rigidbody2D rb;
public float speed;
public float jumpHeight;
public Transform groundCheck;
bool isGrounded;
Animator anim;
int curHp;
int maxHp = 3;
bool isHit = false;
// 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()
{
CheckGround();
if (Input.GetAxis("Horizontal") == 0 && (isGrounded))
{
anim.SetInteger("State", 1);
}
else
{
Flip();
if (isGrounded)
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(groundCh eck.position, 0.2f);
isGrounded = colliders.Length > 1;
if (!isGrounded)
anim.SetInteger("State", 3);
}
public void RecountHp(int deltaHp)
{
curHp = curHp + deltaHp;
if (deltaHp < 0)
{
StopCoroutine(OnHit());
isHit = true;
StartCoroutine(OnHit());
}
print(curHp);
if (curHp <= 0)
{
GetComponent<CapsuleCollider2D>().e nabled = false;
}
}
IEnumerator OnHit()
{
if (isHit)
GetComponent<SpriteRenderer>().colo r = new Color(1f, GetComponent<SpriteRenderer>().colo r.g - 0.04f, GetComponent<SpriteRenderer>().colo r.b - 0.04f);
else
GetComponent<SpriteRenderer>().colo r = new Color(1f, GetComponent<SpriteRenderer>().colo r.g + 0.04f, GetComponent<SpriteRenderer>().colo r.b - 0.04f);
if (GetComponent<SpriteRenderer>().col or.g <= 1)
StopCoroutine(OnHit());
if (GetComponent<SpriteRenderer>().col or.g <= 0)
isHit = false;
yield return new WaitForSeconds(0.02f);
StartCoroutine(OnHit());
}
}
Код, который я креплю к энэми:
Синтаксис:
Используется csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
collision.gameObject.GetComponent<P layer>().RecountHP(-1);
collision.gameObject.GetComponent<R igidbody2D>().AddForce(transform.up * 9f, ForceMode2D.Impulse);
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
collision.gameObject.GetComponent<P layer>().RecountHP(-1);
collision.gameObject.GetComponent<R igidbody2D>().AddForce(transform.up * 9f, ForceMode2D.Impulse);
}
}
}
https://www.programmersforum.ru/attachment.php?attachmentid=98634&stc=1&thumb=1&d=1577360910
https://www.programmersforum.ru/attachment.php?attachmentid=98635&stc=1&d=1577360915