Godot Version
4.4.1
Question
Hi,
I’m having trouble initially positioning a RigidBody3D
object via code. I’ve read up on it as much as I can and tried various approaches. The prevailing wisdom seems to suggest that because it’s a RigidBody3D you can’t just change the position, you need to do it as part of _integrate_forces
. I’ve tried this, and what’s odd is that it seems to work if I set the initial position to be not too far from Vector3.ZERO
(such as Vector3(10, 1, 10)
), but if I set it to something farther away I get a strange effect where the object “bounces back” to its original position.
I’m using DAShoe1’s excellent Godot-Easy-Vehicle-Physics template project as a starting point. In demo_arcade.tscn
the vehicle’s initial position is Vector3.ZERO
.
The changes I’ve made to his code are as follows. I add a top level variable in vehicle.gd
like so:
var teleport_position : Vector3
I then update _integrate_forces
so it looks like this:
func _integrate_forces(state : PhysicsDirectBodyState3D):
current_gravity = state.total_gravity # <-- Not added by me
if teleport_position:
state.linear_velocity = Vector3.ZERO
state.angular_velocity = Vector3.ZERO
state.transform = Transform3D.IDENTITY.translated(teleport_position)
teleport_position = Vector3()
And then I attach a script to ArcadeDemo
at the root of the scene with the following code:
extends Node3D
func _ready() -> void:
# Setting the position to the value below seems to work as expected.
# $VehicleController/VehicleRigidBody.teleport_position = Vector3(10, 1, 10)
# But setting it to this, we end up with the vehicle being moved to that
# position, and then something moves it "back"?
$VehicleController/VehicleRigidBody.teleport_position = Vector3(50, 1, 10)
If I uncomment the first portion and comment the second it works fine, but with the second one uncommented there’s a weird “bounce back” effect. I’ve created all this in a repo that can be downloaded to easily reproduce the issue.
Many thanks for any help.
Tom