当前位置:网站首页>Unity keeps the camera behind and above the player
Unity keeps the camera behind and above the player
2022-07-07 05:34:00 【iiiiiiimp】
Unity Let the camera follow the player all the time
Write it at the front
There is a need , Let the camera follow the player all the time , And rotate with the player's rotation .
Camera follow effect

Principle explanation
1、 Use the coordinates of the camera to get the coordinates of the player to get the upward and backward distance of the camera relative to the player 
The corresponding code
camera2PlayerDir = this.transform.position-playerTransform.position;
playerUp = camera2PlayerDir.y;
playerBack= camera2PlayerDir.z;
2、 Use the position after the player moves + The upward distance of the camera relative to the player ×Vector3.up+ The backward distance of the camera relative to the player ×playerTransform.forward( The current direction of the player ) You can get the position where the camera is going to move 
The corresponding code
targetPosition = playerTransform.position + Vector3.up * playerUp + playerTransform.forward * playerBack;
3、 Use Vector3.Lerp Make the camera move more balanced . The degree of camera rotation is the degree of player rotation .
Randomly generate enemy effect

Principle explanation
In order that the generated enemies will not overlap , That is, two tanks collide in the same place , Need to use Physics.CheckSphere This function .
First, the map boundary and the ground Layer Set up Plane
Then use... In the code LayerMask layerMask = ~LayerMask.GetMask("Plane"); Let the ray not detect Plane Objects at this level
LayerMask layerMask = ~LayerMask.GetMask("Plane");// Equivalent to ~(1<<6)
do {
randomX = Random.Range(-createX, createX);
randomY = Random.Range(-createY, createY);
} while (Physics.CheckSphere(new Vector3(randomX,0,randomY),createRadius,layerMask));
Written in the back
learn Unity Math is really important !!!!
边栏推荐
- Mapbox Chinese map address
- 《2》 Label
- Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
- 人体传感器好不好用?怎么用?Aqara绿米、小米之间到底买哪个
- 1. AVL tree: left-right rotation -bite
- 数字化如何影响工作流程自动化
- JD commodity details page API interface, JD commodity sales API interface, JD commodity list API interface, JD app details API interface, JD details API interface, JD SKU information interface
- Leetcode (417) -- Pacific Atlantic current problem
- nodejs获取客户端ip
- Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET
猜你喜欢
随机推荐
If you want to choose some departments to give priority to OKR, how should you choose pilot departments?
说一说MVCC多版本并发控制器?
5. 数据访问 - EntityFramework集成
Leetcode (417) -- Pacific Atlantic current problem
JVM(十九) -- 字节码与类的加载(四) -- 再谈类的加载器
“多模态”概念
张平安:加快云上数字创新,共建产业智慧生态
CentOS 7.9 installing Oracle 21C Adventures
Mapbox Chinese map address
Mysql database learning (8) -- MySQL content supplement
消息队列:消息积压如何处理?
TabLayout修改自定义的Tab标题不生效问题
论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】
《4》 Form
漏电继电器JD1-100
High voltage leakage relay bld-20
分布式事务介绍
Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET
实现网页内容可编辑
Two methods of thread synchronization








