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:


