当前位置:网站首页>Unity让摄像机一直跟随在玩家后上方
Unity让摄像机一直跟随在玩家后上方
2022-07-06 23:35:00 【iiiiiiimp】
写在前面
有一个需求,让摄像机一直跟随在玩家后上方,并且随玩家的旋转而旋转。
相机跟随效果

原理讲解
1、用摄像机的坐标将去玩家的坐标就能得到摄像机相对于玩家向上和向后的距离
对应代码
camera2PlayerDir = this.transform.position-playerTransform.position;
playerUp = camera2PlayerDir.y;
playerBack= camera2PlayerDir.z;
2、用玩家移动后的位置+摄像机相对于玩家向上的距离×Vector3.up+摄像机相对于玩家向后的距离×playerTransform.forward(玩家当前的朝向)就能得到摄像机要移动到的位置
对应代码
targetPosition = playerTransform.position + Vector3.up * playerUp + playerTransform.forward * playerBack;
3、使用Vector3.Lerp让摄像机移动更加平衡。摄像机旋转的度数就是玩家旋转的度数。
随机生成敌人效果

原理讲解
为了让生成的敌人不会重叠,即生成在同一个地方导致两个坦克碰撞到一起,需要用到Physics.CheckSphere这个函数。
首先将地图边界和地面的Layer设置Plane
然后在代码中使用LayerMask layerMask = ~LayerMask.GetMask("Plane");让射线不检测Plane这个层级的物体
LayerMask layerMask = ~LayerMask.GetMask("Plane");//等价于~(1<<6)
do {
randomX = Random.Range(-createX, createX);
randomY = Random.Range(-createY, createY);
} while (Physics.CheckSphere(new Vector3(randomX,0,randomY),createRadius,layerMask));
写在后面
学Unity数学真的很重要!!!!
边栏推荐
- K6EL-100漏电继电器
- How Alibaba cloud's DPCA architecture works | popular science diagram
- pmp真的有用吗?
- LabVIEW is opening a new reference, indicating that the memory is full
- Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
- Pytest testing framework -- data driven
- JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
- 最长不下降子序列(LIS)(动态规划)
- Timer创建定时器
- 2039: [蓝桥杯2022初赛] 李白打酒加强版 (动态规划)
猜你喜欢

If you‘re running pod install manually, make sure flutter pub get is executed first.

10 distributed databases that take you to the galaxy

K6EL-100漏电继电器

JHOK-ZBL1漏电继电器

Error: No named parameter with the name ‘foregroundColor‘

照片选择器CollectionView

Intelligent annotation scheme of entity recognition based on hugging Face Pre training model: generate doccano request JSON format

张平安:加快云上数字创新,共建产业智慧生态

CentOS 7.9 installing Oracle 21C Adventures

【js组件】自定义select
随机推荐
Is PMP really useful?
AIDL 与Service
SQL injection HTTP header injection
HarmonyOS第四次培训
【js组件】自定义select
《2》 Label
最长不下降子序列(LIS)(动态规划)
Is it necessary to renew the PMP certificate?
Two person game based on bevy game engine and FPGA
U++ metadata specifier learning notes
c语言神经网络基本代码大全及其含义
阿里云的神龙架构是怎么工作的 | 科普图解
LinkedBlockingQueue源码分析-初始化
Timer创建定时器
Longest common subsequence (LCS) (dynamic programming, recursive)
一条 update 语句的生命经历
Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
batch size设置技巧
Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET
全链路压测:影子库与影子表之争