Make Camera3D face the same direction as RigidBody3D.+ direction-based movement system

4.2

Question

Hi everybody, I wanna start by saying that this is my first time working with a 3D environment so i might use some wrong terms, make logical errors, etc…

I’m trying to create a game like Super Monkey Ball, so I have a ball (RigidBody3D), some platforms (StaticBody3D) and a Camera3D and I’m currently having 2 problems in the development.

First Problem

At the moment the Camera is controlled by a script that changes his position just by editing the Ball’s position in order to create an offset/distance effect.

Now I’m trying to edit the Camera script in order to make the Camera always face the direction in which the ball is going towards, but I just can’t find a way to make it work.

Second Problem

As of right now i’m using a simple script for moving the ball: using the arrow keys I can apply an impulse using some predefined Vector3 (left, right, forward, backwards), but with the new camera I also need a new system where i can either follow the current direction (the new forward), go in the opposite direction (the new backwards), or go left/right (making a 90deg angle from the vector of the current direction of the ball).

For this problem I can’t understand how to get the Vector3 of the current direction and how to apply the 90deg angle in order to then implement the left/right movement.

Script

``````
var DIRECTIONAL_VELOCITY_COEFFICENT = 0.25
var SHIFT_DOWN_ENABLED = true
var GOING_BACK_ENABLED = true

func _physics_process(delta: float) -> void:
#\$Camera3D.look_at(\$RigidBody3D.position)
\$Camera3D.position.x = \$RigidBody3D.position.x
\$Camera3D.position.y = \$RigidBody3D.position.y + 1.6
\$Camera3D.position.z = \$RigidBody3D.position.z + 2.7

# Going out of bounds
if \$RigidBody3D.position.y < -25:
\$RigidBody3D.freeze = true
\$RigidBody3D.position = Vector3(0,0,0)
\$RigidBody3D.freeze = false

func _input(event: InputEvent) -> void:
"""
if event.is_action_pressed("jump"):
jump()
if event.is_action_pressed("left"):
turn_left()
if event.is_action_pressed("right"):
turn_right()
if event.is_action_pressed("front"):
go_front()
if event.is_action_pressed("back"):
go_back()
"""

if event is InputEventKey:
if event.pressed and event.keycode == KEY_ESCAPE:
get_tree().quit()
if event.pressed and event.keycode == KEY_LEFT:
turn_left()
if event.pressed and event.keycode == KEY_RIGHT:
turn_right()
if event.pressed and event.keycode == KEY_UP:
go_front()
if event.pressed and event.keycode == KEY_DOWN and GOING_BACK_ENABLED:
go_back()
if event.pressed and event.keycode == KEY_SHIFT and SHIFT_DOWN_ENABLED:
jump_down()

if event.is_action_pressed("jump"):
jump()

func jump():
\$RigidBody3D.apply_impulse(Vector3(0, 30, 0))
func turn_left():
var vel = \$RigidBody3D.linear_velocity.x
\$RigidBody3D.apply_impulse(Vector3(-3 - (abs(vel) * DIRECTIONAL_VELOCITY_COEFFICENT), 0, 0))
func turn_right():
var vel = \$RigidBody3D.linear_velocity.x
\$RigidBody3D.apply_impulse(Vector3(3 + (abs(vel) * DIRECTIONAL_VELOCITY_COEFFICENT), 0, 0))
func go_front():
\$RigidBody3D.apply_impulse(Vector3(0, 0, -3))
func go_back():
\$RigidBody3D.apply_impulse(Vector3(0, 0, 3))
func jump_down():
\$RigidBody3D.apply_impulse(Vector3(0, -30, 0))
``````