Объектный код игры
Код: Выделить всё
Animator animator;
Rigidbody2D rb;
public float knockbackDamping = 0.5f;
public float health = 3;
public float Health { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public void Start()
{
animator = GetComponent();
rb = GetComponent();
} public void TakeDamge(float damage)
{
throw new System.NotImplementedException();
health -= damage;
if (health = 0)
{
animator.SetTrigger("hit");
}
}
public void OnHit(float damage, Vector2 knockback)
{
health -= damage;
rb.AddForce(new Vector3(knockback.x, knockback.y, 0f));
}
public void Defeated()
{
Destroy(gameObject);
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.tag == "sword")
{
OnHit(1, Vector2.zero);
Debug.Log("Slime получил урон!");
}
}
public void TakeDamage(float damage)
{
throw new System.NotImplementedException();
}
void Update()
{
rb.velocity *= knockbackDamping;
}
Кодекс меча
`
Код: Выделить всё
Vector2 movementInput;
public Collider2D swordcollider;
public float swordDamage = 1f;
public float knockbackForce = 30f;
Vector2 rightAttackOffset;
public Vector3 faceright = new Vector3(0.1f, -0.1f, 0);
public Vector3 faceleft = new Vector3(-0.5f, -0.1f, 0);
void Start()
{
swordcollider = GetComponent();
rightAttackOffset = transform.position;
if (swordcollider == null) {
Debug.LogWarning("Sword collider not set");
}
}
public void AttackRight()
{
print("AttackR");
swordcollider.enabled = true;
transform.position = rightAttackOffset;
}
public void AttackLeft()
{
print("AttackL");
swordcollider.enabled = true;
transform.position = new Vector3(rightAttackOffset.x * -1, rightAttackOffset.y);
}
private void SetSwordOffset(bool isFacingRight)
{
if (isFacingRight)
{
swordcollider.transform.localPosition = faceright;
}
else
{
swordcollider.transform.localPosition = faceleft;
}
}
public void IsFacingRight(bool isFacingRight)
{
SetSwordOffset(isFacingRight);
}
public float damage = 10;
private void OnTriggerEnter2D(Collider2D other)
{
IDamagable damagable = other.GetComponent();
if (damagable != null) {
Vector3 parentPosition = gameObject.GetComponentInParent().position;
Vector2 direction = (Vector2)(other.gameObject.transform.position - parentPosition).normalized;
Vector2 knockback = direction * knockbackForce;
damagable.OnHit(swordDamage, knockback);
} else
{
Debug.LogWarning("Collider cant");
}
}
Код: Выделить всё
public interface IDamagable
public float Health { get; set; }
void OnHit (float damage, Vector2 knockback );
void TakeDamage(float damage);
Подробнее здесь: https://stackoverflow.com/questions/791 ... trigger-is
Мобильная версия