Hi, I’ve recently updated to Godot 4.3, and am trying to understand how physics work with CharacterBody3Ds. I have crates that I can pick up and drop, and when I drop them, they use gravity, but as soon as a single part of them touches the “floor” they don’t continue to be affected by gravity, leading to some impossible situations. Here is the code for the crate that can be picked up:
extends CharacterBody3D
var item_name = "Crate"
var is_held = false
@onready var player = $"../Player"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
velocity = Vector3.ZERO
#Hold
if is_held == true:
self.reparent(player.carry_point)
global_rotation.x = 0
global_rotation.z = 0
if global_position.distance_to(player.carry_point.global_position) > 0.1 and global_position.distance_to(player.carry_point.global_position) < 1:
velocity = ((player.carry_point.global_position - global_position).normalized()) * 10
elif global_position.distance_to(player.carry_point.global_position) >= 1:
is_held = false
#Drop
elif is_held == false:
self.reparent(player.get_parent())
if !is_on_floor():
velocity += get_gravity()/1.8
elif is_on_floor():
velocity = Vector3.ZERO
move_and_slide()
func hold():
is_held = !is_held
Character bodies are not purely physics driven objects like a RigidBody would be. Your code does not specify to rotate the box based on it’s floor collision, so it does not rotate off the other box.
I think there are many tutorials on YouTube on drag and drop physics, you can check them.
Otherwise RigidBody3D uses force as velocity, but you can also define the velocity in a RigidBody3D like this:
var velocity = Vector3.ZERO
func _ready():
gravity_scale = 0.0
func _physics_process(delta):
move_and_slide(delta) #call it whenever needed
func move_and_slide(delta):
position += velocity * delta
But I am not sure it will work and also it is not the proper way, so I consider to look for some tutorials on YouTube.
Hi, I used this tutorial, and it works for the most past, unfortunately, the crate jitters quite violently once it reaches the correct location. Here’s the current code snippet of code:
Yes you removed the 10 times force multiplier, I’d still recommend removing the if statement above.
# storing long difference calculation
var difference: Vector3 = player.carry_point.global_position - global_position
# removed if statement, if the length is 0.1 or lower it will move very slowly
linear_velocity = difference.limit_length(1.0) * 10
# gettings the squared length is faster
if difference.length_squared() >= 1:
is_held = false
Hi, this works well for carrying the object, however, if the object hits something, it will gain rotational velocity, which never stops, is there a way to fix this? Video below: