在2d空间上,假设角色的自身的y轴方向为正方向,如果要让角色随时面向一个目标点。

这里假设(0,0)点为目标点

 

第一种:

Vector3 v = Vector3.zero - transform.position;                               //首先获得目标方向
v.z = 0;                                                                                            //这里一定要将z设置为0
float angle = Vector3.SignedAngle(Vector3.up,v,Vector3.forward);//得到围绕z轴旋转的角度
Quaternion rotation = Quaternion.Euler(0, 0, angle);                     //利用角度得到rotation
transform.rotation = rotation;

 

第二中:

Vector3 v = Vector3.zero - transform.position;
v.z = 0;
Quaternion rotation = Quaternion.FromToRotation(Vector3.up, v);
transform.rotation = rotation;

Unity2D 面向目标方向

 

相关文章:

  • 2021-11-14
  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2021-10-27
  • 2022-01-31
  • 2021-08-19
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
  • 2021-07-06
  • 2021-07-14
  • 2022-12-23
相关资源
相似解决方案