摄像机平滑跟随

using UnityEngine;
using System.Collections;


//更新位置的委托
delegate void UpdatePosition();

public class CameraFollow : MonoBehaviour {

    public Transform target;

    //需要锁定的坐标(无法实时生效)
    public bool freazeX, freazeY, freazeZ;


    //跟随的平滑时间(类似于滞后时间)
    public float smoothTime = 0.3f;
    private float xVelocity, yVelocity, zVelocity = 0.0f;

    private Vector3 offset;

    //全局缓存的位置变量
    private Vector3 oldPosition;

    private Vector3 startPosition;

    private UpdatePosition JudgePosition;


    void Start ()
    {
        startPosition = transform.position;
        offset = transform.position - target.position;

        //事件
        if (!freazeX)
        {
            JudgePosition += MoveX;
        }
        if (!freazeY)
        {
            JudgePosition += MoveY;
        }
        if (!freazeZ)
        {
            JudgePosition += MoveZ;
        }
    }

   void LateUpdate ()
    {
        oldPosition = transform.position;
        JudgePosition();

        transform.position = oldPosition;    

   }

  
    private void MoveX()
    {
        //Mathf.SmoothDamp(三个重载):   Gradually changes a value towards a desired goal over time.
        //current:          The current position.
        //target:           The position we are trying to reach.
        //currentVelocity: The current velocity, this value is modified by the function every time you call it.
        //smoothTime:    Approximately the time it will take to reach the target. A smaller value will reach the target faster.
        //maxSpeed:     Optionally allows you to clamp the maximum speed.
        //deltaTime:     The time since the last call to this function. By default Time.deltaTime.
        oldPosition.x = Mathf.SmoothDamp(transform.position.x, target.position.x + offset.x, ref xVelocity, smoothTime);
    }


    private void MoveY()
    {
        oldPosition.y = Mathf.SmoothDamp(transform.position.y, target.position.y + offset.y, ref yVelocity, smoothTime);
    }


    private void MoveZ()
    {
        oldPosition.z = Mathf.SmoothDamp(transform.position.z, target.position.z + offset.z, ref zVelocity, smoothTime);
    }


    /// <summary>
    /// 用于重新开始游戏时重置相机位置
    /// </summary>
    public void ResetPosition()
    {
        transform.position = startPosition;
    }
}
在Unity中,实现摄像机平滑移动有多种方法,以下为你介绍几种常见的方式: ### 使用 `Mathf.Lerp` 实现简单线性平滑移动 `Mathf.Lerp` 函数可在给定的起始值和目标值之间进行线性插值,返回介于两者之间的插值结果,可用于简单的线性平滑移动。不过,此函数通常用于处理单个数值的插值,若要实现摄像机位置的平滑移动,需对位置的每个分量(x、y、z)分别进行插值。 ### 使用 `Vector3.Lerp` 实现摄像机平滑跟随 以下是一个使摄像机平滑跟随目标物体,并保持目标物体在摄像机中心位置的示例代码: ```csharp using UnityEngine; public class FollowCamera : MonoBehaviour { public Transform target; // 跟随的目标物体 public float damping = 1; // 跟随平滑程度 private void LateUpdate() { if (target != null) { // 计算目标位置在屏幕上的坐标 Vector3 targetPosition = Camera.main.WorldToViewportPoint(target.position); // 计算摄像机应该移动的向量 Vector3 delta = target.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, targetPosition.z)); // 计算摄像机的目标位置 Vector3 destination = transform.position + delta; // 使用平滑阻尼移动摄像机 transform.position = Vector3.Lerp(transform.position, destination, damping * Time.deltaTime); } } } ``` 在该代码里,通过调整 `damping` 变量的值,能够控制跟随平滑程度 [^2]。 ### 使用 `Vector3.SmoothDamp` 实现平滑移动 `Vector3.SmoothDamp` 函数可让物体在一段时间内平滑地移动到目标位置,适用于需要更自然平滑效果的场景。以下是示例代码: ```csharp using UnityEngine; using UnityEngine.UI; public class CameraSmoothMove : MonoBehaviour { public Camera camera; public Transform tarPos; public bool cameraMove = false; private Vector3 velocity = Vector3.zero; public Button btn; void Start() { btn.onClick.AddListener(delegate { cameraMove = true; }); } void Update() { if (cameraMove) { camera.position = Vector3.SmoothDamp(camera.position, tarPos.position, ref velocity, 2f); camera.rotation = Quaternion.RotateTowards(camera.rotation, tarPos.rotation, Time.deltaTime * 20); } } } ``` 在上述代码中,当按钮被点击时,`cameraMove` 变为 `true`,摄像机开始平滑移动到目标位置 `tarPos`,同时进行平滑旋转 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值