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

Debug.DrawRay导致Player对象Velocity异常增高的问题求助

Unity中Debug.DrawRay引发Player速度异常的问题解决

问题现象

在Unity项目中使用Debug.DrawRay后,Player游戏对象的Velocity会大幅增高,移除该行代码后异常消失,但需要保留射线用于调试。

问题根源

代码中存在关键错误:在射线相关调用里直接修改了transform.position,例如:

Debug.DrawRay(transform.position -= new Vector3(5, 0, 0), -transform.up, Color.green, 1);

以及多个Physics2D.Raycast调用里的transform.position -= new Vector3(-5, 0, 0)这类操作。

transform.position -= xxx是赋值操作,会直接改变Player的实际位置,每帧Update执行一次就会让Player持续偏移。当强制修改Transform位置时,Unity物理引擎会尝试同步Rigidbody2D的位置和速度,进而导致Velocity异常飙升——本质上不是Debug.DrawRay的问题,是错误地用它修改了物体位置。

修复方案

不要直接修改transform.position,而是创建临时Vector3变量存储偏移后的射线起点,既不影响Player实际位置,也能正常绘制调试射线和检测地面。

修正后的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static Unity.Collections.AllocatorManager;

public class Player : MonoBehaviour
{
    public float Speed;
    public float JumpForce;
    private Rigidbody2D rb;
    public bool OnGround;
    public bool Jump;
    public bool MobileJump;

    private bool MoveLeftBool;
    private bool MoveRightBool;

    public LayerMask Blocks;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        OnGround = false;

        MoveLeftBool = false;
        MoveRightBool = false;
    }

    void Update()
    {
        // 用临时变量存储射线起点,不修改transform.position
        var leftRayStart = transform.position + new Vector3(-5, 0, 0);
        var rightRayStart = transform.position + new Vector3(5, 0, 0);
        
        // 调试射线正常绘制,不影响物体位置
        Debug.DrawRay(rightRayStart, -transform.up, Color.green, 1);

        Jump = Input.GetKey(KeyCode.Space);

        // 地面检测改用临时变量
        bool leftOnGround = Physics2D.Raycast(leftRayStart, -transform.up, 1, Blocks);
        bool rightOnGround = Physics2D.Raycast(rightRayStart, -transform.up, 1, Blocks);
        bool centerOnGround = Physics2D.Raycast(transform.position, -transform.up, 1, Blocks);
        
        // 任意射线检测到地面即判定为在地面
        OnGround = leftOnGround || rightOnGround || centerOnGround;
    }

    public void MoveLeft()
    {
        MoveLeftBool = true;
    }

    public void MoveRight()
    {
        MoveRightBool = true;
    }

    public void StopMoving()
    {
        MoveLeftBool = false;
        MoveRightBool = false;
        rb.velocity = new Vector2(0, rb.velocity.y);
    }

    public void JumpMethod()
    {
        MobileJump = true;
    }
    public void StopJumpMethod()
    {
        MobileJump = false;
    }

    void FixedUpdate()
    {
        if (Jump && OnGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, 0);
            rb.AddForce(new Vector2(0, JumpForce * 10));
            Jump = false;
        }
        if (MobileJump && OnGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, 0);
            rb.AddForce(new Vector2(0, JumpForce * 10));
        }

        if (Input.GetKey(KeyCode.A) || MoveLeftBool)
        {
            rb.velocity = new Vector2(-Speed, rb.velocity.y);
        }
        else if (Input.GetKey(KeyCode.D) || MoveRightBool)
        {
            rb.velocity = new Vector2(Speed, rb.velocity.y);
        }
        else
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Spike"))
        {
            Destroy(gameObject);
        }
    }
}

额外优化说明

  • 原代码多次设置OnGround = true/false会导致后续检测覆盖前面结果,改用逻辑或合并三个射线检测结果更合理。
  • Jump = Input.GetKey(KeyCode.Space);简化了原有的if-else判断,代码更简洁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 22:35:13