You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity 2D游戏对象间X/Y速度保存与传递问题求助

问题解决:角色切换时速度同步失效

问题分析

当前实现中,切换角色时虽然设置了目标对象的Rigidbody2D速度,但存在两个核心问题:

  1. 输入逻辑直接覆盖速度:Player脚本的Update方法每次都会将moveVelocity初始化为0,再重新赋值,导致切换后设置的速度被立即覆盖;Ball脚本在无方向输入时不会修改X轴速度,但Player脚本的逻辑会强制重置X速度。
  2. 组件引用可能未正确赋值:如果Unity编辑器的Inspector面板中,ballRB2D或playerRB2D没有拖入对应对象的Rigidbody2D组件,速度设置操作会完全失效。

修复方案

步骤1:确认组件引用正确

分别选中Player和Ball对象,在对应的脚本组件面板中,将ballRB2D、playerRB2D字段拖入对应对象的Rigidbody2D组件,确保引用有效。

步骤2:修改脚本逻辑,保留切换时的速度

添加bool标记,用于在切换后的第一帧保留传递的速度,避免被输入逻辑覆盖。

修改后的PlayerScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public GameObject ball1;
    public GameObject player1;
    public Rigidbody2D ballRB2D;

    public Rigidbody2D rb2d;

    public float speed;
    public float jump;
    float moveVelocity;

    public bool grounded = true;
    private bool justSwitched = false; // 标记刚切换到当前角色

    void OnTriggerEnter2D(Collider2D other)
    {
        grounded = true;
    }

    private void OnTriggerStay(Collider2D other)
    {
        grounded = true;
    }

    void Start()
    {
        justSwitched = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ball1.transform.position = transform.position;
            ballRB2D.velocity = rb2d.velocity;
            // 给Ball标记刚切换状态
            var ballScript = ball1.GetComponent<BallScript>();
            if(ballScript != null)
            {
                ballScript.justSwitched = true;
            }

            ball1.SetActive(true);
            player1.SetActive(false);
        }

        if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (grounded)
            {
                rb2d.velocity = new Vector2(rb2d.velocity.x, jump);
                grounded = false;
            }
        }

        moveVelocity = 0;

        // 刚切换时,保留传递的速度,不重置X轴
        if (!justSwitched)
        {
            if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
            {
                moveVelocity = -speed;
            }
            if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
            {
                moveVelocity = speed;
            }

            rb2d.velocity = new Vector2(moveVelocity, rb2d.velocity.y);
        }
        else
        {
            // 下一帧恢复正常输入逻辑
            justSwitched = false;
        }
    }
}

修改后的BallScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BallScript : MonoBehaviour
{
    public Rigidbody2D rb2d;
    public float moveSpeed = 5;
    public GameObject player;
    public GameObject ball;
    public Rigidbody2D playerRB2D;

    public bool justSwitched = false; // 标记刚切换到当前角色

    void Start()
    {
        justSwitched = false;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            player.transform.position = transform.position;
            playerRB2D.velocity = rb2d.velocity;
            // 给Player标记刚切换状态
            var playerScript = player.GetComponent<PlayerScript>();
            if(playerScript != null)
            {
                playerScript.justSwitched = true;
            }

            player.SetActive(true);
            ball.SetActive(false);
        }

        // 刚切换时,保留传递的速度,不修改X轴
        if (!justSwitched)
        {
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                rb2d.velocity = new Vector2(-moveSpeed, rb2d.velocity.y);
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
                rb2d.velocity = new Vector2(moveSpeed, rb2d.velocity.y);
            }
            // 无输入时保持当前X速度
        }
        else
        {
            // 下一帧恢复正常输入逻辑
            justSwitched = false;
        }
    }
}

额外优化建议

  • 将角色切换逻辑抽成单独的管理脚本,避免两个脚本各自处理输入,降低耦合度
  • 原脚本中OnTriggerEnter2D和OnTriggerStay方法缺少Collider2D other参数,会导致地面检测逻辑失效,已在修改后的代码中补充

内容的提问来源于stack exchange,提问作者OnACow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 05:57:01