Instantiated scene does not rotate with the parent as pivot

Godot Version

Version 4.4

Question

I am starting to learn Godot by creating a game in which my ‘Player’ scene is instantiating and adding the ‘Ball’ scene as it child on starting the game. I position the Ball at a distance from the player. The ball is moving along with the player but not rotating with the player as the pivot.

Player scene:

Ball scene:

player.gd:

extends CharacterBody3D

@export var speed = 14
@export var sensitivity = 0.008
@export var ball : PackedScene 

var target_velocity = Vector3.ZERO

func _ready() -> void:
	var ballPos = $Rig.position
	ballPos.z += 5
	inst_ball(ballPos)

func inst_ball(pos):
	var instance = ball.instantiate()
	instance.position = pos
	add_child(instance)
	

func _physics_process(delta: float):
#	Movement:
	var direction = Vector3.ZERO
	
	if Input.is_action_pressed("move_fwd"):
		direction.z += 1
	if Input.is_action_pressed("move_bwd"):
		direction.z -= 1
	if Input.is_action_pressed("move_left"):
		direction.x += 1
	if Input.is_action_pressed("move_right"):
		direction.x -= 1
	if Input.is_action_pressed("move_up"):
		direction.y += 1
	if Input.is_action_pressed("move_down"):
		direction.y -= 1
		
	if direction != Vector3.ZERO:
		direction = direction.normalized()
		direction = $Rig.global_transform.basis * direction
		
	target_velocity = direction * speed
	velocity = target_velocity
	move_and_slide()

func _input(event: InputEvent) -> void:
	if event is InputEventMouseMotion:
		rotation.y -= event.relative.x * sensitivity
		rotation.x += event.relative.y * sensitivity

Game when started:

The ball as a child of the player is probably not a good idea, if the player moves left/right so will the ball. You can use add_sibling instead to add the ball without making it a child. One reason it may be failing to inherit rotation is that RigidBodies are supposed to be driven by physics forces, not the parent’s transform; say gravity is pushing the ball into the floor and the parent rotates, the rigidbody will calculate the gravity/floor collision and respond by keeping the ball in it’s current location, overwriting the parent’s rotational change

1 Like

Oh! I didn’t know that info about RigidBodies.

I want the ball to move left/right as the player moves.

If I try freezing the ball(someone suggested at another site), it solves the issue but then the ball cannot collide with any object.
In my case collision is not a problem when player has the ball. But I’m not sure if freezing the ball is the right thing to do. Would it bring up any other issues?

Freezing sounds good until you want to move the ball again. You could un-freeze and reparent the ball at the same time.

var ball: RigidBody3D # keep track of your ball after instantiating

func drop_ball() -> void:
    ball.freeze = false
    ball.reparent(get_parent()) # change to siblling
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.