Unity 2D 物体旋转指向目标


public
class RoleStand : PlayerAction { PlayerControl playerCtrl = null; public override ActionType Name => ActionType.Stand; public override bool IsCanMove => false; public override bool IsCtrlFree => false; public override void Enter(IPlayerControl _player, EventParam _param) { playerCtrl = _player as PlayerControl; playerCtrl._animator.Play("idle", true); } public override void Exit() { } public override void _Update() { if (XJoystick.IsInput) { Vector3 dir = new Vector3(XJoystick.AxisData.x, XJoystick.AxisData.y,0); float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); } } }

Vector2 direction = target.transform.position - transform.position;
  float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

效果如下:

posted @ 2020-08-25 17:37  大刀乱飞  阅读(743)  评论(0)    收藏  举报