You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Unity 2D平台游戏空中移动时角色下落异常缓慢的问题求助

Unity 2D平台游戏空中移动时角色下落异常缓慢的问题求助

我正在用Unity做一个2D平台游戏,现在在加跳跃功能。但是当玩家在空中移动的时候,角色下落得特别慢,一旦我停止空中行走,它又会快速下落!我不知道怎么解决这个问题!我没有自己写重力代码,用的是Rigidbody2D自带的重力。

以下是我写的Movement脚本代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Movement : MonoBehaviour
{
    public float speedright = 5;
    public Rigidbody2D rb;
    public float speedleft = -50;
    public Text e;
    Scene currentScene;
    string sceneName;
    public float jumpheight = 300;
    public Talking script;

    void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
        rb.constraints = RigidbodyConstraints2D.FreezeRotation;
        bool Seen = false;
        Invoke("Starting", 3);
        if(Seen == true)
        {
            script.CanMove = 1;
        }
    }

    private void OnTriggerStay2D(Collider2D collider)
    {
        if (sceneName == "Bedroom"){
            e.text = "E To Interact";
            e.gameObject.SetActive(true);
            if(Input.GetKeyDown("e"))
            {
                SceneManager.LoadScene("Hallway");
            }
        }
    }

    private void OnTriggerExit2D()
    {
        e.text = "";
    }

    void Update()
    {
        if (script.CanMove == 1)
        {
            if (Input.GetKey("d"))
            {
                rb.velocity = new Vector2(speedright, 0 * Time.deltaTime);
                rb.transform.eulerAngles = new Vector3(0, 0, 0);
            }
            if (Input.GetKey("a"))
            {
                rb.velocity = new Vector2(speedleft, 0 * Time.deltaTime);
                rb.transform.eulerAngles = new Vector3(0, 180, 0);
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                rb.AddForce(Vector2.up * jumpheight, ForceMode2D.Impulse);
            }
        }
    }
}

问题分析与解决方案

兄弟,我一眼就瞅到问题根源了!你在处理左右移动的时候,把刚体的Y轴速度直接强制清零了!

看你这段代码:

rb.velocity = new Vector2(speedright, 0 * Time.deltaTime);

还有A键对应的移动代码,每次按下移动键,你都把rb.velocity的Y分量设成了0——这就等于在空中移动时,直接抵消了Rigidbody2D自带的重力下落速度,角色自然就慢腾腾飘着;等你松开按键,Y轴速度不再被强制重置,重力立刻正常生效,角色就会突然加速下落,这就是你看到的怪异现象。

修复起来超简单,只要修改左右移动的代码,保留当前的Y轴速度,只更新X轴的移动速度就行:

void Update()
{
    if (script.CanMove == 1)
    {
        if (Input.GetKey("d"))
        {
            // 保留原本的Y轴速度,只替换X轴
            rb.velocity = new Vector2(speedright, rb.velocity.y);
            rb.transform.eulerAngles = new Vector3(0, 0, 0);
        }
        if (Input.GetKey("a"))
        {
            // 同样保留Y轴速度
            rb.velocity = new Vector2(speedleft, rb.velocity.y);
            rb.transform.eulerAngles = new Vector3(0, 180, 0);
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(Vector2.up * jumpheight, ForceMode2D.Impulse);
        }
    }
}

另外提个小建议:你把speedleft设成了-50,和speedright的5差距太大了,左右移动速度会严重不对称,建议改成-5保持一致,游戏体验会更好~

备注:内容来源于stack exchange,提问作者user22411435

火山引擎 最新活动