码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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 脚本

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class SmoothDamp : MonoBehaviour
    5. {
    6. public Transform target;
    7. public float smoothTime = 0.3F;
    8. private float yVelocity = 0.0F;
    9. // Start is called before the first frame update
    10. void Start()
    11. {
    12. }
    13. // Update is called once per frame
    14. void Update()
    15. {
    16. //缓动 y 轴上的位置
    17. float newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y, ref yVelocity, smoothTime);
    18. transform.position = new Vector3(transform.position.x, newPosition, transform.position.z);
    19. }
    20. }

    2.绑定对象

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

    3.播放

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

  • 相关阅读:
    MATLAB | R2023b更新了哪些好玩的东西?
    20232831 袁思承 2023-2024-2 《网络攻防实践》第7次作业
    iOS CocoaPod 打包:SDK开发、Pod组件生成等
    分库分表真实案例,扩容10倍容量
    Java#21(抽象与接口)
    第四章:Python中的字典(下)
    尚硅谷MySQL学习笔记
    【设计模式】中介者模式
    华为云HECS云服务器docker环境下安装nacos
    DeepAR多重多元多步时间序列预测
  • 原文地址:https://blog.csdn.net/shenqiangda/article/details/133888985
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号