当前位置:网站首页>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数学真的很重要!!!!
边栏推荐
- Two person game based on bevy game engine and FPGA
- 说一说MVCC多版本并发控制器?
- 漏电继电器JOLX-GS62零序孔径Φ100
- DOM node object + time node comprehensive case
- Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
- Scheduledexecutorservice timer
- [JS component] date display.
- 局部变量的数组初始化问题
- 1.AVL树:左右旋-bite
- Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET
猜你喜欢
QT simple layout box model with spring
数字化创新驱动指南
Safe landing practice of software supply chain under salesforce containerized ISV scenario
一条 update 语句的生命经历
漏电继电器LLJ-100FS
pmp真的有用吗?
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
10 distributed databases that take you to the galaxy
利用OPNET进行网络仿真时网络层协议(以QoS为例)的使用、配置及注意点
拿到PMP认证带来什么改变?
随机推荐
Pytest testing framework -- data driven
Scheduledexecutorservice timer
When deleting a file, the prompt "the length of the source file name is greater than the length supported by the system" cannot be deleted. Solution
[Oracle] simple date and time formatting and sorting problem
Where is NPDP product manager certification sacred?
【最佳网页宽度及其实现】「建议收藏」
MySQL数据库学习(8) -- mysql 内容补充
What changes will PMP certification bring?
线程池的创建与使用
As we media, what websites are there to download video clips for free?
AIDL 与Service
实现网页内容可编辑
DFS,BFS以及图的遍历搜索
Creation and use of thread pool
Let f (x) = Σ x^n/n^2, prove that f (x) + F (1-x) + lnxln (1-x) = Σ 1/n^2
HarmonyOS第四次培训
Tencent cloud database public cloud market ranks top 2!
【js组件】自定义select
Timer create timer
qt 简单布局 盒子模型 加弹簧