相机跟随CameraFollow

此博客展示了一个相机跟随脚本的代码。该脚本使用Unity引擎开发,定义了相机与目标的距离、高度等参数,通过计算旋转角度和高度,实现相机在LateUpdate方法中跟随目标,并在Start和Update方法中获取目标对象,保证相机持续跟随。

/***
 * 
 *  相机跟随脚本 
 * 
 * 
 * 
 * 
 * 
 * 
 */
using UnityEngine;
using System.Collections;

namespace Globals
{
    public class CameraFollow : MonoBehaviour
    {
        // The target we are following
        public Transform target = null;
        // The distance in the x-z plane to the target
        public float distance = 6.0f;  //原来 6
        // the height we want the camera to be above the target
        public float height = 8.0f;//原来 8
        // How much we 
        public float heightDamping = 4.0f;
        public float rotationDamping = 0.0f;

        void LateUpdate()
        {
            // Early out if we don't have a target
            if (!target)
                return;
            // Calculate the current rotation angles
            var wantedRotationAngle = target.eulerAngles.y;
            var wantedHeight = target.position.y + height;
            var currentRotationAngle = transform.eulerAngles.y;
            var currentHeight = transform.position.y;
            // Damp the rotation around the y-axis
            currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
            // Damp the height
            currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

            // Convert the angle into a rotation
            Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
            // Set the position of the camera on the x-z plane to:
            // distance meters behind the target
            transform.position = target.position;
            transform.position -= currentRotation * Vector3.forward * distance;
            // Set the height of the camera
            //transform.position.y = currentHeight;       
            transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
            // Always look at the target
            transform.LookAt(target);
            // Use this for initialization
        }
        void Start()
        {
            target = GameObject.FindGameObjectWithTag("Player").transform;
        }

        float a = 2.0f;
        // Update is called once per frame
        void Update()
        {
            a -= Time.deltaTime;
            if (a <= 0)
            {
                target = GameObject.FindGameObjectWithTag("Player").transform;
                a = 2.0f;

            }
        }
    }//class_end
}

在Unity中,有多种常见的相机跟随模式,下面为你详细介绍: ### 带有跟随角度控制的相机跟随 这种模式是对基础相机跟随的改进,在原有基础上添加了跟随角度的控制。相机相对于玩家有一个固定的偏移位置,通过`Vector3.Lerp`方法调整相机与玩家之间的距离,同时使用`Quaternion.Slerp`方法获取并调整相机的旋转角度,使相机始终朝向玩家。以下是示例代码: ```csharp using UnityEngine; using System.Collections; public class CameriaTrack : MonoBehaviour { private Vector3 offset = new Vector3(0,5,4); // 相机相对于玩家的位置 private Transform target; private Vector3 pos; public float speed = 2; // Use this for initialization void Start () { target = GameObject.FindGameObjectWithTag("Player").transform; } // Update is called once per frame void Update () { pos = target.position + offset; this.transform.position = Vector3.Lerp(this.transform.position, pos, speed*Time.deltaTime); // 调整相机与玩家之间的距离 Quaternion angel = Quaternion.LookRotation(target.position - this.transform.position); // 获取旋转角度 this.transform.rotation = Quaternion.Slerp(this.transform.rotation, angel, speed * Time.deltaTime); } } ``` 这种模式能让相机跟随玩家的同时,动态调整角度,保持对玩家的良好视角[^1]。 ### 一直观察角色背部的相机跟随 此模式下相机一直观察角色的背部,并且可以通过鼠标滚轮控制相机的远近。相机的位置由角色的位置、向上的偏移量和向后的偏移量决定,使用`Vector3.Lerp`方法平滑移动相机位置,使用`transform.LookAt`方法让相机始终朝向角色。示例代码如下: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollow : MonoBehaviour { public Transform target; public float distanceUp = 5f; public float distanceAway = 5f; public float smooth = 2f; // 位置平滑移动值 public float camDepthSmooth = 2f; // Use this for initialization void Start() { } // Update is called once per frame void Update() { // 鼠标轴控制相机的远近 if ((Input.mouseScrollDelta.y < 0 && Camera.main.fieldOfView >= 3) || Input.mouseScrollDelta.y > 0 && Camera.main.fieldOfView <= 80) { Camera.main.fieldOfView += Input.mouseScrollDelta.y * camDepthSmooth * Time.deltaTime; } } void LateUpdate() { // 相机的位置 Vector3 disPos = target.position + Vector3.up * distanceUp - target.forward * distanceAway; transform.position = Vector3.Lerp(transform.position, disPos, Time.deltaTime * smooth); // 相机的角度 transform.LookAt(target.position); } } ``` 该模式适用于第三人称视角游戏,能为玩家提供稳定的背后视角,同时允许一定的相机距离调整[^2]。 ### 基础的带有角度调整的相机跟随 这种模式中,相机相对于玩家有一个固定的偏移位置,在`Start`方法中计算该偏移量。在`Update`方法中,通过`Vector3.Lerp`方法调整相机与玩家之间的距离,使用`Quaternion.Slerp`方法调整相机的旋转角度,使相机朝向玩家。示例代码如下: ```csharp private Vector3 offset; // 相机相对于玩家的位置 public Transform target; private Vector3 pos; public float speed = 2; private void Start() { offset = transform.position - target.position; } // Update is called once per frame void Update() { pos = target.position + offset; this.transform.position = Vector3.Lerp(transform.position, pos, speed * Time.deltaTime); // 调整相机与玩家之间的距离 Quaternion angel = Quaternion.LookRotation(target.position - transform.position); // 获取旋转角度 transform.rotation = Quaternion.Slerp(transform.rotation, angel, speed * Time.deltaTime); } ``` 此模式是一种基础的相机跟随方式,能保证相机跟随玩家并保持相对位置和朝向[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值