unity中 canvas下物体的朝向跟随

发布时间:2024年01月06日

? ? public Transform target;


? ? private Vector3 direction;
? ? void Update()
? ? {
? ? ? ? //第一种
? ? ? ? //direction = target.position - transform.position;
? ? ? ? //transform.up = -direction.normalized;

? ? ? ? //第二种
? ? ? ? if (target != null )
? ? ? ? {
? ? ? ? ? ? // 获取目标物体的方向
? ? ? ? ? ? Vector3 direction = target.position -transform.position;

? ? ? ? ? ? // 计算朝向角度? 其中90f根据实际需求调整
? ? ? ? ? ? float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg+90f;

? ? ? ? ? ? // 更新UI物体的朝向
? ? ? ? ? ?transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
? ? ? ? }

? ? ? ? //if (target != null)
? ? ? ? //{
? ? ? ? // ? ?// 获取目标物体位置并使当前物体朝向目标物体
? ? ? ? // ? ?Vector3 targetDirection = target.position - transform.position;
? ? ? ? // ? ?transform.rotation = Quaternion.LookRotation(targetDirection, );
? ? ? ? //}


? ? }

文章来源:https://blog.csdn.net/u011146831/article/details/135382453
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。