赞
踩
本脚本为一个目标跟随
unity中3dUI或者模型始终面向摄像机,跟随摄像机视角旋转
使用方法超级简单,对于一个资深的Ctrl+cv程序极为友好
不多讲解直接上代码
public class Focus : MonoBehaviour { // The target we are following [SerializeField] private Transform target; [SerializeField] private bool once; [SerializeField] private bool reverse; // The distance in the x-z plane to the target [SerializeField] private float distance = 10.0f; private void OnEnable() { Follow(); } void LateUpdate() { if (once) return; Follow(); } private void Follow() { var forward = target.TransformDirection(Vector3.forward); transform.position = target.position + forward * distance; var toward = Quaternion.identity; toward.SetLookRotation(!reverse ? target.forward : -target.forward, target.up); transform.rotation = toward; } }
我们的Target挂载摄像机
Once这个bool:为true时挂载的物体被激活时执行一次 效果为出现固定位置并固定悬浮
Reverse这个bool值功能为:挂载此脚本的物体反转
Dixtance为:出现后的距离
简单又实用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。