当前位置:网站首页>unity中3dUI或者模型始终面向摄像机,跟随摄像机视角旋转丨视角跟随丨固定视角
unity中3dUI或者模型始终面向摄像机,跟随摄像机视角旋转丨视角跟随丨固定视角
2022-07-22 18:07:00 【Glunn】
视角跟随
本脚本为一个目标跟随
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为:出现后的距离
简单又实用
边栏推荐
猜你喜欢

Construction training camp module II operation

大一暑假实习day6_拼图游戏

Code random notes_ Linked list_ 142 circular linked list II

Detailed explanation of Axi agreement

Detailed explanation of advanced data types in C language

Druid source code read the status in 9-druiddatasource and druidconnection

APB protocol details vs. 3.0-4.0-5.0

Basic operation of linked list

Code random notes_ Linked list_ 160 intersecting linked list

leetcode-188. 买卖股票的最佳时机 IV
随机推荐
链表的基本操作
二进制SCA指纹提取黑科技:Go语言逆向技术
Understand OOP and common interview questions
IDEA使用指南
二叉树—堆
Ctfshow_web entry information collection CSDN creation punch in;
代码随想录笔记_链表_160相交链表
Programming Xiaobai's first blog
C语言链表详解 & 两类重要链表的实现
大一暑假实习day5_3
超详解 - 如何理解C语言中while(scanf(“%d“, &num) != EOF)这一表达式?
递归函数实现素数判断
Detailed explanation of C language linked list & Implementation of two important linked lists
代码随想录笔记_链表_19两两交换链表中的节点
Idea usage guide
AHB agreement related
Construction training camp module I operation
The heavy performance of karma in the four training of Li Fan
Detailed explanation of Axi agreement
Operation on December 1