37 lines
983 B
C#
37 lines
983 B
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Entity
|
|
{
|
|
public class Bullet : Entity
|
|
{
|
|
public override void SetTarget(Vector3 pos)
|
|
{
|
|
base.SetTarget(pos);
|
|
RotateTransformToDirection(transform, direction);
|
|
}
|
|
|
|
protected override void AutoBehave()
|
|
{
|
|
TryMove();
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
other.GetComponent<Entity>()?.OnHit(this);
|
|
attributes.health -= 1;
|
|
}
|
|
// 旋转对象到指定方向
|
|
public static void RotateTransformToDirection(Transform transform, Vector3 targetDirection)
|
|
{
|
|
// 默认向上方向
|
|
Vector3 upDirection = Vector3.up;
|
|
|
|
// 计算旋转角度
|
|
float angle = Mathf.Atan2(targetDirection.x, targetDirection.y) * Mathf.Rad2Deg;
|
|
|
|
// 设置旋转
|
|
transform.rotation = Quaternion.Euler(0, 0, angle);
|
|
}
|
|
}
|
|
} |