当前位置:网站首页>Unity 获取当前物体正前方,一定角度、距离的坐标点
Unity 获取当前物体正前方,一定角度、距离的坐标点
2022-06-28 08:03:00 【Star_MengMeng】
1.有时场景中需要让固定物体或Canvas生成在角色正前方(VR中比较常用)


代码很简单:
Vector3 targetPos = rig.transform.forward * 2f + rig.transform.position;
wui.transform.position = targetPos;
wui.transform.LookAt(rig.transform);
还可以加上角度:
public Vector3 GetPosition(Quaternion rotation, Vector3 position, float distance)
{
Vector3 direction = rotation * Vector3.forward;
return position + (direction * distance);
}
如果是对象是Canvas(world Space模式),注意画布的正反,可以通过修改scale改变画布的翻转:如,gui.transform.localScale=Vector3(-1,1,1)
2.比如我想获取某个点在我正前方的角度值(需要同一层级的节点,不同层级需要先转换坐标)
public class Example : MonoBehaviour
{
public float angleBetween = 0.0f;
public Transform target;
void Update()
{
Vector3 targetDir = target.position - transform.position;
angleBetween = Vector3.Angle(transform.forward, targetDir);
}
}
Vector3相减获得方向向量,然后和transform.forward取角度。
3.注意这两个常用forward的区别

边栏推荐
- Ambari (VIII) --- ambari integrated impala document (valid for personal test)
- HJ base conversion
- HJ prime factor
- HJ string sort
- SQL analysis (query interception analysis for SQL optimization)
- Section 9: dual core startup of zynq
- Sentinel mechanism of redis cluster
- 你了解TCP协议吗(一)?
- Evaluation of inverse Polish expression < difficulty coefficient >
- sql主从复制搭建
猜你喜欢

Airflow2 configuration windows azure SSO details based on oauth2 protocol

Trigonometric transformation formula

Prometheus monitoring (I)

MySQL installation and environment variable configuration

ES6 use of return in arrow function

22/02/14 study notes

22/02/15 study notes

Prometheus service discovery

Ambari (VIII) --- ambari integrated impala document (valid for personal test)

HJ质数因子
随机推荐
SOC serial port configuration
协程、asyncio、异步编程
MySQL single table access method
Flutter realizes the function of "shake"
SQL Master slave Replication Build
Prometheus monitoring (I)
股票炒股注册开户靠谱吗?安全吗?
Safety training is the greatest benefit for employees! 2022 induction safety training for new employees
[shangpinhui] project notes
打新债注册开户靠谱吗?安全吗?
Redis persistence problem and final solution
How to configure DDR3 of dm8148
Section 5: zynq interrupt
sql主从复制搭建
SOC timer and interrupt configuration
Software design of power control board
Introduction to kubernetes (I)
HJ string sort
SLAM中常用的雅克比矩阵J
Cloud native: cloud computing technology is upgraded again to open an era of comprehensive cloud development