0 votes

Hi, I am following a tutorial on letting a camera follow behind a character that moves forwards, backwards, strafes left/right with the keyboard keys, and changes direction with the mouse movements.

I am able to use the provided Gdscript for the camera to make the camera follow the character as it moves.

However, it seems that the instructions on making the camera rotate with the character is missing. i am guessing this is because the camera is rotating about itself instead of rotating with the character as the pivot.

What is a simple way to achieve this?

Tutorial: https://kidscancode.org/godot_recipes/g101/3d/101_3d_03/

My Camera.gd:

extends Camera

onready var target: Object = get_parent().get_node("Player")
export var smooth_speed: float
export var offset: Vector3

func _process(delta):
    if (target != null):
        self.transform.origin = target.transform.origin + offset

I tried to make it rotate by adding 1 more line:

extends Camera

onready var target: Object = get_parent().get_node("Player")
export var smooth_speed: float
export var offset: Vector3

func _process(delta):
    if (target != null):
        self.transform.origin = target.transform.origin + offset
        self.rotation = target.rotation

but now its all messed up.

How can we fix the camera code so that it rotates with the character? Thanks!

enter image description here

enter image description here

My Player.gd:
extends KinematicBody

var gravity = Vector3.DOWN * 12     # strength of gravity
var speed = 4   # movement speed
var jump_speed = 6  # jump strength
var spin = 0.1  # rotation speed

var velocity = Vector3.ZERO
var jump = false

func get_input():
    velocity.x = 0
    velocity.z = 0

    if Input.is_action_pressed("move_forward"):
        velocity.z -= speed
    if Input.is_action_pressed("move_back"):
        velocity.z += speed
    if Input.is_action_pressed("strafe_right"):
        velocity.x += speed
    if Input.is_action_pressed("strafe_left"):
        velocity.x -= speed

func _physics_process(delta):
    velocity += gravity * delta
    get_input()
    velocity = move_and_slide(velocity, Vector3.UP)

func _unhandled_input(event):
    if event is InputEventMouseMotion:
        if event.relative.x > 0:
            rotate_y(-lerp(0, spin, event.relative.x/10))
        elif event.relative.x < 0:
            rotate_y(-lerp(0, spin, event.relative.x/10))
Godot version 3.4.2.stable
in Engine by (12 points)

There is a class called RemoteTransform. You can apply it to the Camera, set path to player, and choose to only copy rotation. However is this really what You want to do ? I can't imagine such camera in any game. You will lose vision of your player at multitude of his angles. You propably want to rotate camera around only one axis. Try to imagine and carefully design behavior of camera rotation first.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.