Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | nyxynyx |
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!
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))
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.
Inces | 2022-02-25 14:10