当前位置:网站首页>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的区别
边栏推荐
- asp. Net upload image path and image name
- Ambari (VII) --- ambari integrated hue4.2 document (valid for personal test)
- How redis solves cache avalanche, breakdown and penetration problems
- sql主從複制搭建
- Airflow2 configuration windows azure SSO details based on oauth2 protocol
- Recommended system series (Lecture 5): Optimization Practice of sorting model
- asp. Net datalist when there are multiple data displays
- ES6 use of return in arrow function
- [JS] - [throttling and anti shake function]
- Software design of power control board
猜你喜欢
Redis one master multi slave cluster setup
flex布局
8 figures | analyze Eureka's first synchronization registry
HJ prime factor
Do you know TCP protocol (1)?
Upgrade HDP spark to spark 2.4.8 without upgrading ambari
sql主從複制搭建
Section VII starting principle and configuration of zynq
你了解TCP協議嗎(二)?
Introduction to kubernetes (I)
随机推荐
Study notes 22/1/10
Estimation of SQL execution cost by MySQL query optimizer
Sentinel mechanism of redis cluster
How to configure DDR3 of dm8148
GPIO configuration of SOC
Install haproxy
Host is not allowed to connect to this MySQL server
Soft test -- software designer -- database design of afternoon questions
HJ21 简单密码
Hash slot of rediscluster cluster cluster implementation principle
Is it reliable for flush to register and open an account? Is it safe?
asp. Net upload image path and image name
Study notes 22/1/19 and 22/1/20
Hj21 simple password
Prometheus + grafana + MySQL master-slave replication + host monitoring
Redis cerebral fissure
Is it reliable to open a new bond registration account? Is it safe?
pip 更新到最新的版本
你了解TCP协议吗(一)?
asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...