`Hi - I have a RigidBody3D in my scene which i want to interact with the player by following a node in 3D space.
I have succeeded in achieving this, however i would also like for the RigidBody3D to always be rotated, so that it retains the same rotation (relative to the player) as it did prior to beginning the interaction.
example: if the box is interacted with while the player is standing perfectly in front of one of it’s faces, I want the box to attempt to rotate so that when interaction is finished, the player would still be standing in front of that same face.
Here is my code:`
interactableBox.gd
extends RigidBody3D
@onready var raycast_target : Node3D = $"/root/test_world/Character/Head/Camera/interactionRay/target"
@onready var player : CharacterBody3D = $"/root/test_world/Character"
@export var speed = 10
var default_gravity = gravity_scale
var interactedWith : bool = false
# raycast: RayCast3D = get_node("/root/test_world/Charater/Head/Camera/interactionRay")
# Called when the node enters the scene tree for the first time.
func _ready():
player.interact.connect(_on_player_interact)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
#do stuff when interacted with
if (interactedWith == true):
if position.distance_to(raycast_target.global_position) != 0.0:
linear_velocity = (raycast_target.global_position - global_transform.origin) * speed
if position.distance_to(raycast_target.global_position) == 0.0:
linear_velocity *= 0
func _on_player_interact(interactable):
# flip-flop the values
if ( Input.is_action_just_pressed("interact") and interactedWith == false and interactable == self):
interactedWith = true
gravity_scale = 0.0
elif ( Input.is_action_just_pressed("interact") and interactedWith == true):
interactedWith = false
gravity_scale = default_gravity
that should mean that you don’t have a variable called player (of type Node3D or any inherited node types) that is set to the player or that the node you tun the script from does not hire Node3D.
I thought so too, which is why i made sure that i assign the variable at the right place, so i moved it inside of _process(): since that’s what runs once the object is available and able to call player, which fixed that error. However, i now get an error of Error setting property 'rotation' with value of type float in the line of code reading rotation = global_rotation.y + relative_rotation
unfortunately, linear_velocity = (raycast_target.global_position - global_transform.origin) * speed doesn’t work well with anything that modifies rotation, so the cube does not move for as long as anything happens to global_rotation
It seemed like that to me, too, however it’s clearly not working.
instead what happens is it remembers the position it should follow, and only attempts to follow it once it’s no longer being interacted with, leading to it gainining incredible speed like a slingshot once you let go.
Strange. Does your use case allow to use a CharacterBody3D instead of a rigidbody? If so there should be no problem with setting the velocity and rotation at the same time.
i see in the godot documentation that RigidBody3D nodes use forces and impulses to move and rotate. i will try to implement what im looking for with those instead of changing position values