Godot Version
4.2.2
Question
I’m trying to make my player throw a card when a button on the controller/keyboard is pressed. To achieve this, I am dynamically instantiating a Rigidbody3D card at the player’s Raycast3D position and applying an impulse to make the card fall to the ground when thrown. However, it’s not working properly, the card is floating in the air, and the player is being pushed backward for no reason:
Here is my code, i don’t know what’s wrong with it:
Player class:
func throw_card(card_id: int):
var instance: Object = preload("res://scenes/env/card_physic_item.tscn")
var instance_card: CardPhysicItem = instance.instantiate() #instantiate the scene of the card dynamically
instance_card.position = ray_cast_3d.global_position #set the position and basis of where raycast is pointing
instance_card.transform.basis = ray_cast_3d.global_transform.basis
instance_card.update_card(card_id) #update the id of the card
get_parent().add_child(instance_card) #add it to the scene
pass
Card class:
func update_card(id_card: int):
await self.ready #wait until the card is not ready
item_sprite.frame = id_card #change the sprite based on id of the card
apply_card_impulse() #apply the impulse to rigidbody which is the card in this case
pass
func apply_card_impulse()->void:
animation_player.play("throw_card") # Play an animation
apply_central_impulse(Vector3(0, 0, -1000)) # Apply a central impulse to the rigidbody
print("applied card impulse")
pass
Could someone help me figure out how to solve this? Or does anyone know another approach that yields the same result? Thank you very much