Unity 2D 物体旋转指向目标

Mathf.Atan2 反正切2

static function Atan2 (y : float, x : float) : float

Description描述

Returns the angle in radians whose Tan is y/x.

以弧度为单位计算并返回 y/x 的反正切值。返回值表示相对直角三角形对角的角,其中 x 是临边边长,而 y 是对边边长。

Return value is the angle between the x-axis and a 2D vector starting at zero and terminating at (x,y).

返回值为x轴和一个零点起始在(x,y)结束的2D向量的之间夹角。

// Usually you use transform.LookAt for this.
//通常使用transform.lookAt.
// But this can give you more control over the angle
//但这可以给你更多的对角度的控制

Mathf.Rad2Deg 弧度转度

绕axis轴旋转angle,创建一个旋转。

Vector2 direction = target.transform.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
//这里还可以使用 Quaternion.Lerp来实现平滑旋转
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值