当前位置:网站首页>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的区别

边栏推荐
- Is it reliable to register and open an account for stock speculation? Is it safe?
- Ambari (VIII) --- ambari integrated impala document (valid for personal test)
- Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
- 异或的应用。(提取出数字中最右侧的1,面试中经常用的到)
- Ambari (IX) --- use expect to realize no interaction in ambri server setup phase (valid for personal test)
- Co process, asyncio, asynchronous programming
- Study notes 22/1/18
- SQL Master slave Replication Build
- 8 figures | analyze Eureka's first synchronization registry
- Update pip to the latest version
猜你喜欢

Upgrade HDP spark to spark 2.4.8 without upgrading ambari

Resizing node of rediscluster cluster cluster mode

8 figures | analyze Eureka's first synchronization registry

Do you know TCP protocol (2)?

kubernetes集群命令行工具kubectl

SOC serial port configuration

Kubernetes理论基础

Airflow2 configuration windows azure SSO details based on oauth2 protocol

Jacobian matrix J commonly used in slam

Prometheus service discovery
随机推荐
HJ字符串排序
Resizing node of rediscluster cluster cluster mode
Airflow2.1.1 ultra detailed installation document
Activity implicit jump
Upgrade HDP spark to spark 2.4.8 without upgrading ambari
软件测试与质量期末复习
Hj21 simple password
Airflow2.x distributed deployment DAG execution failure log cannot be obtained normally
sql主從複制搭建
NLP sequence can completely simulate human brain intelligence
HJ prime factor
Section 9: dual core startup of zynq
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
MySQL two table connection principle (understand join buf)
Is it reliable for flush to register and open an account? Is it safe?
22/02/14 study notes
HJ整数与IP地址间的转换
Redis cluster deployment and application scenarios
Prometheus service discovery
Study notes 22/1/19 and 22/1/20