Skip to main content

Command Palette

Search for a command to run...

Unity 跳跃学习笔记

Published
1 min read
Unity 跳跃学习笔记

在 Unity 中实现跳跃的方式有多种,我整理了三种可行的方式,在这里分享一下。

Transform

关键代码为:

public float jumpHeight = 2f;
transform.position = new Vector3(transform.position.x, transform.position.y + jumpHeight, 0);

也就是直接修改物体的位置,说实话,单这一行根本算不上是跳跃,应该是瞬移。

所以要实现 y 坐标的渐进变化,应该用缓动函数来计算位置,线性变化可以用 Vector3.Lerp 来实现:

private Vector3 startPosition;
private Vector3 endPosition;
private float jumpStartTime;
public float jumpTime = 0.5f;
private bool isJumping = false;
public float jumpHeight = 2f;

void Update()
{
    if (!isJumping && Input.GetButtonDown("Jump"))
    {
        isJumping = true;
        startPosition = transform.position;
        endPosition = transform.position + new Vector3(0, jumpHeight, 0);
        jumpStartTime = Time.time;
        StartCoroutine(Jump());
    }
}

IEnumerator Jump()
{
    while (Time.time < jumpStartTime + jumpTime)
    {
        float t = (Time.time - jumpStartTime) / jumpTime;
        transform.position = Vector3.Lerp(startPosition, endPosition, t);
        yield return null;
    }

    isJumping = false;
}

代码中能控制的地方有 jumpHeightjumpTime

velocity

关键代码为:

rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(rb.velocity.x, jumpHeight);

原理是给 rigidbody 一个 y 轴正向的速度,会让 rigidbody 自行处理向上的移动,然后受到重力影响,自行下落。

AddForce

关键代码为:

rb = GetComponent<Rigidbody2D>();
rb.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Impulse);

原理是给 rigidbody 一个向上的力,Impulse 表示冲量,让力只存在一瞬间,然后 rigidbody 会获得加速度向上冲。

velocity 和 AddForce 的区别

在一段跳时,两者没有明显区别,在二段跳时,两者的移动路径是不同的。

AddForce 的跳跃高度会受按键间隔影响,间隔越小,跳得越高。因为 AddForce 是施加力到物体上,短期内施加多个力,会让他们叠加,从而带来更大的加速度。

同理,间隔越长,二段跳的效果越不明显,因为此时只受重力影响,加速度向下,新的力带来的加速度会被抵消掉。

Z
zgd1y ago

We're looking for experienced MMO mobile game developers who have successfully developed multiple large-scale multiplayer online role-playing games (MMORPGs) at renowned gaming companies. This is a remote position offering highly competitive salaries with flexible working hours. Candidates can arrange their own schedules, and we're willing to provide an advance deposit upfront. If you meet these criteria and are interested, please contact us directly.

EMail:q791864008q@gmail.com Discord:q791864008q_gmail_com Telegram:q791864008q_gmail_com

Z
zgd1y ago

Who has the source code of a large multiplayer online mobile game that was completed within the last 5 years? This game must possess a carefully designed commercialization numerical system. I am willing to pay a high fee to purchase it for learning and research purposes. email:q791864008q@gmail.com. If the game meets our expectations, we are willing to pay an additional recognition fee in advance.

Z
zgd1y ago

Who has access to the source code for a completed, large-scale multiplayer online Android and iOS game developed by a major gaming company? The game must have top-notch graphics. I will not accept games developed by individuals. I am willing to pay a high fee to purchase it for learning and research purposes. Contact me q791864008q@gmail.com

Z
zgd1y ago

Who has the source code of a mature and large-scale multiplayer online mobile game from a major game company? I'm willing to pay a super high fee to buy Contact me q791864008q@gmail.com

More from this blog

12 月装机行动记录

(Banner 图文无关) 这几天买了新的装备回来升级配置,除了显卡和散热器,其他都更新了,这两个不更新的原因是太贵了。 由于我不太懂选配置,所以还是让朋友给推荐,我说我的预算在 5000 左右,他就给我转了一个整机,配置大概是这样的: CPU:AMD 9700X 主板:微星 B650M GAMING PLUS WIFI 或者 微星 B650M GAMING WIFI 显卡:木有 内存:英睿达/宇瞻 DDR5 6000 32G 硬盘:1T NVMe PCIe4 SSD 读速 3500M...

Dec 7, 20241 min read
12 月装机行动记录

Homekit + cozylife 插座连接 HA

近期在淘宝上买了个 Homekit + cozylife 的插座,就这种: 一开始只通过 iOS 访问,就是只连接 Homekit,长按开关重置插座,iOS 一扫码就连上了,后来我嫌在外面访问不了,又不想掏钱买苹果的 HomePod,于是就装上了 Home Assistant,打算让设备们都连上 HA,这样就不用交苹果税了。 连接方式还是通过 Homekit,一般来说支持 Homekit 设备都能这样连接,先连上 iOS,然后在 Home App 中移除设备,这时候就能在 HA 中找到设备了:...

Nov 30, 20241 min read
Homekit + cozylife 插座连接 HA

找到了一台祖传的 Ccd 相机

开个玩笑,这台相机其实是我们家在 05 年的时候买的,发票都还在呢,当时花了 4000 块钱!搁现在我都受不了,更别说当年了,看到价格我都震惊了。 相机的型号是索尼的 Cybershot DSC-N1,属于小红书时尚单品 CCD 相机,由于一直放在包装盒里,现在还有 99 新呢。 机子还是正常的,能开机,能拍照,其中一个问题是日期,这款没有 WiFi 功能,所以时间只能保存在本地,不知道是不是 BUG,每次开机都让我重新设置,默认就定在 2005 年 1 月 1 日。 第二个问题是电池,电池应该...

Nov 28, 20241 min read
找到了一台祖传的 Ccd 相机

记录和 ffmpeg 与 LLM 搏斗的两天

要做的 最近在写一个制作视频的功能,就是把 N 个视频合并,然后把对应的 N 张图片,在视频开始的前 5 秒叠加显示出来。 第一口 - diffusion studio 本来我用的是 diffusion studio,这是一个 JS 库,但这玩意性能太差了,因为他要把视频每一帧都读到 canvas 里,数据一多页面就卡住了(为啥要折腾 DOM 呢?) 而且他的 API 十分不好用,作为浏览器脚本你无法读本地数据也就算了,你起码给一个接受纯数据的参数吧,比如 HTML 类型接受源代码,Image ...

Nov 16, 20242 min read
记录和 ffmpeg 与 LLM 搏斗的两天
V

void mian

39 posts