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);