当前位置: 首页 > news >正文

Unity3D 基础——使用 Mathf.SmoothDamp 函数制作相机的缓冲跟踪效果

使用 Mathf.SmoothDamp 函数制作相机的缓冲跟踪效果,让物体的移动不是那么僵硬,而是做减速的缓冲效果。将以下的脚本绑定在相机上,然后设定好 target 目标对象,即可看到相机的缓动效果。通过设定 smoothTime 的值,可以调节缓动效果的持续时间。

Mathf-SmoothDamp - Unity 脚本 APIicon-default.png?t=N7T8https://docs.unity.cn/cn/current/ScriptReference/Mathf.SmoothDamp.html

Mathf.SmoothDamp

public static float SmoothDamp (float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed= Mathf.Infinity, float deltaTime= Time.deltaTime);

参数

current当前位置。
target尝试达到的目标。
currentVelocity当前速度,此值由函数在每次调用时进行修改。
smoothTime达到目标所需的近似时间。值越小,达到目标的速度越快。
maxSpeed可以选择允许限制最大速度。
deltaTime自上次调用此函数以来的时间。默认情况下为 Time.deltaTime。

1.创建 SmoothDamp 脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SmoothDamp : MonoBehaviour
{public Transform target;public float smoothTime = 0.3F;private float yVelocity = 0.0F;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){//缓动 y 轴上的位置float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);}
}

2.绑定对象

将 SmoothDamp 脚本添加到 Main Camera 对象上;然后设置 Target 参数对象,调节 Target 对象的位置使其与 Main Camera 对象的高度不一样。

3.播放

单机播放按钮,可以看到主相机的高度缓缓移动到 Target 的高度。

http://www.lryc.cn/news/196629.html

相关文章:

  • leetcode-200. 岛屿数量
  • python的搜索引擎系统设计与实现 计算机竞赛
  • Unity随笔:在Unity中使用多线程需要注意什么
  • SQL Select(选择) 语法
  • Python武器库开发-基础篇(二)
  • 在 CentOS 8.2 上安装 MySQL C/C++ 客户端库 libmysqlclient.so
  • 『C++ - STL』之优先级队列( priority_queue )
  • 简述什么是服务端包含(Server Side Include)?
  • 领英如何注册?2023超全面详细教程
  • Spring Cloud Gateway 使用 Redis 限流使用教程
  • Qt事件系统 day7
  • 微服务拆分的思考
  • DateUtil工具类记录
  • 可信执行环境简介:ARM 的 TrustZone
  • 【音视频流媒体】 3、ffmpeg、ffplay、ffprobe 超详细介绍
  • 解决kong部署自定义插件报 helloworld plugin is enabled but not installed
  • 动态数据源自定义SqlSessionFactoryBean时mybatis plus配置失效
  • 【Qt控件之QDialogButtonBox】概述及使用
  • IPv6知识概述 - ND协议
  • react-redux的connect函数实现
  • Vue3使用Vite创建项目
  • NCV7724DQBR2G车规级半桥电机驱动芯片-专为汽车,工业自动化应用提供完美解决方案
  • NSS [GWCTF 2019]枯燥的抽奖
  • 微信小程序会议OA系统
  • CICD:Circle CI 实现CICD
  • 竞赛 深度学习YOLO安检管制物品识别与检测 - python opencv
  • 【华为OD机试python】斗地主之顺子【2023 B卷|100分】
  • ant design DatePicker禁用之前的时间
  • C语言---预处理详解
  • 数组和对象有什么区别?