godot4

How do I make a RigidBody3D node player controllable in Godot?


I am trying to make a game where the main character is a ball who cannot jump and only can roll. The problem is that since I am using the CharacterBody3D node, the player cannot be pushed around by other objects, cannot roll at all (therefore cannot use ramps), and is not what I want.

Instead of simply using the CollisionShape3D node, I tried using the StaticBody3D node as a parent to the CollisionShape3D node, but this just made the player clip through everything. I watched this tutorial, but I STILL could not figure it out. If anyone can help, thank you!


Solution

  • Use RigidBody3D for your player ball. It will correctly collide with other static and rigid bodies.

    enter image description here

    Script:

    extends RigidBody3D
    
    func _process(delta: float) -> void:
      if Input.is_key_pressed(KEY_UP):
        add_constant_force(Vector3.FORWARD)
    
      if Input.is_key_pressed(KEY_DOWN):
        add_constant_force(Vector3.BACK)
    

    Scene source:

    [gd_scene load_steps=8 format=3 uid="uid://c6tmp3j1b2lys"]
    
    [ext_resource type="Script" path="res://player-ball/rigid_body_3d_player.gd" id="1_t5n4v"]
    
    [sub_resource type="BoxShape3D" id="BoxShape3D_brdte"]
    
    [sub_resource type="SphereShape3D" id="SphereShape3D_37r6m"]
    
    [sub_resource type="BoxShape3D" id="BoxShape3D_57wgf"]
    
    [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_oa4yg"]
    
    [sub_resource type="Sky" id="Sky_2t01t"]
    sky_material = SubResource("ProceduralSkyMaterial_oa4yg")
    
    [sub_resource type="Environment" id="Environment_m8s36"]
    background_mode = 2
    sky = SubResource("Sky_2t01t")
    
    [node name="Node3D" type="Node3D"]
    
    [node name="StaticBody3D_Floor" type="StaticBody3D" parent="."]
    axis_lock_linear_x = true
    axis_lock_linear_y = true
    axis_lock_linear_z = true
    axis_lock_angular_x = true
    axis_lock_angular_y = true
    axis_lock_angular_z = true
    
    [node name="CSGBox3D" type="CSGBox3D" parent="StaticBody3D_Floor"]
    transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.1, 0)
    use_collision = true
    size = Vector3(100, 0.2, 100)
    
    [node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D_Floor"]
    transform = Transform3D(10, 0, 0, 0, 10, 0, 0, 0, 10, 0, -10, 3.523)
    shape = SubResource("BoxShape3D_brdte")
    
    [node name="RigidBody3D_Player" type="RigidBody3D" parent="."]
    transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
    script = ExtResource("1_t5n4v")
    
    [node name="CSGSphere3D" type="CSGSphere3D" parent="RigidBody3D_Player"]
    use_collision = true
    
    [node name="CollisionShape3D" type="CollisionShape3D" parent="RigidBody3D_Player"]
    shape = SubResource("SphereShape3D_37r6m")
    
    [node name="StaticBody3D_Jump" type="StaticBody3D" parent="."]
    transform = Transform3D(1, 0, 0, 0, 0.559916, 0.828549, 0, -0.828549, 0.559916, 0, -0.160642, -4.60921)
    
    [node name="CSGBox3D" type="CSGBox3D" parent="StaticBody3D_Jump"]
    
    [node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D_Jump"]
    shape = SubResource("BoxShape3D_57wgf")
    
    [node name="Camera3D" type="Camera3D" parent="."]
    transform = Transform3D(1, 0, 0, 0, 0.974236, 0.225529, 0, -0.225529, 0.974236, 0, 0.925598, 2.1632)
    
    [node name="WorldEnvironment" type="WorldEnvironment" parent="."]
    environment = SubResource("Environment_m8s36")