一、摄像机控制
1.摄像机与物体同步移动
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23public class CameraController : MonoBehaviour { // store a public reference to the Player game object, so we can refer to it's Transform public GameObject player; // Store a Vector3 offset from the player (a distance to place the camera from the player at all times) private Vector3 offset; // At the start of the game.. void Start () { // Create an offset by subtracting the Camera's position from the player's position offset = transform.position - player.transform.position; } // After the standard 'Update()' loop runs, and just before each frame is rendered.. void LateUpdate () { // Set the position of the Camera (the game object this script is attached to) // to the player's position, plus the offset amount transform.position = player.transform.position + offset; } }
2.摄像机跟随物体移动
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public float smoothing = 5f; // The speed with which the camera will be following. Vector3 offset; // The initial offset from the target. void Start () { // Calculate the initial offset. offset = transform.position - target.position; } void FixedUpdate () { // Create a postion the camera is aiming for based on the offset from the target. Vector3 targetCamPos = target.position + offset; // Smoothly interpolate between the camera's current position and it's target position. transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime
最后
以上就是忧虑水杯最近收集整理的关于Unity基础之代码篇 —— 摄像机控制、物体控制、触发器、射线、导航代理等的全部内容,更多相关Unity基础之代码篇内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复