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

Godot 4中VehicleBody3D数字车速表数值异常持续增长问题

Godot 4 VehicleBody3D 车速表数值持续增长问题修复

问题概述

在Godot 4中为VehicleBody3D类型车辆制作屏幕底部数字车速表时,无论车辆加速、减速或静止,标签中的数值都会持续增长。

节点结构

- World
  - Car (VehicleBody3D)
    - Car-Body (MeshInstance3D)
    - Car-Body-Collider (CollisionShape3D)
    - Front-Left-Wheel (VehicleWheel3D)
      - Front-Left-Wheel-Mesh (MeshInstance3D)
    - Front-Right-Wheel (VehicleWheel3D)
      - Front-Right-Wheel-Mesh (MeshInstance3D)
    - Back-Left-Wheel (VehicleWheel3D)
      - Back-Left-Wheel-Mesh (MeshInstance3D)
    - Back-Right-Wheel (VehicleWheel3D)
      - Back-Right-Wheel-Mesh (MeshInstance3D)
  - Track 1 (Node)
    - MeshInstance3D
  - Speedometer (Label)

问题分析与修复步骤

1. 车辆脚本的变量覆盖问题

自动加载的车辆脚本中,_physics_process函数内声明了局部变量speed,导致全局的speed变量从未被更新,车速表无法获取正确的车辆速度值。

修复后的车辆脚本(关键部分修改):

extends VehicleBody3D

# ... 其他变量声明保持不变 ...

var speed  # 全局速度变量

func _physics_process(delta):
    _move_and_rpm_and_sound(delta)
    _camera_switch()

    # 移除var声明,直接给全局speed变量赋值
    speed = linear_velocity.length()
    print(speed)

# ... 其他函数保持不变 ...

2. 地面缺少碰撞体导致车辆持续下落

Track 1节点仅包含MeshInstance3D,没有添加碰撞形状,车辆会因重力持续下落,linear_velocity的y分量不断增大,最终导致速度数值持续增长。

修复操作:

  • 给Track 1节点添加CollisionShape3D子节点
  • 为CollisionShape3D设置与地面Mesh匹配的碰撞形状(例如使用BoxShape3D或ConcavePolygonShape3D)

3. 车速单位转换错误

原车速表脚本中使用speed *= 10进行单位转换,这是不准确的。Godot中linear_velocity的单位是米/秒,转换成**英里/小时(mph)**的正确系数是2.23694。

修复后的车速表脚本:

extends Label

func _ready():
    text = "0 mph"

func _physics_process(_delta):
    # 获取全局脚本中的速度值,转换为mph
    var current_speed = CarScrpt.speed * 2.23694
    text = str(int(current_speed)) + " mph"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:18:10