Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | vockerz |
Hello, I’m new in Godot. I’ve been following this tutorial for my game. but, in this tutorial, the camera is following the rotation of the character. I want to make the camera following like a bird view or an isometric platform game, like Torchlight 2 or Diablo
Torchlight 2 Camera
The script for camera following is
extends Camera
export var distance = 4.0
export var height = 2.0
func _ready():
set_physics_process(true)
set_as_toplevel(true)
func _physics_process(delta):
var target = get_parent().get_global_transform().origin
var pos = get_global_transform().origin
var up = Vector3(0,1,0)
var offset = pos - target
offset = offset.normalized()*distance
offset.y = height
pos = target + offset
look_at_from_position(pos, target, up)
and attached script for player movement is
extends KinematicBody
var gravity = -9.8
var velocity = Vector3()
var camera
var character
const SPEED = 6
const ACCELERATION = 3
const DE_ACCELERATION = 2
func _ready():
#pass
character = get_node(".")
func _physics_process(delta):
camera = get_node("CameraBase/Camera").get_global_transform().basis
var dir = Vector3()
var is_moving = false
if(Input.is_action_pressed("move_fw")):
dir += -camera[2]
is_moving = true
#gimbal.translation.z = character.translation.z
if(Input.is_action_pressed("move_bw")):
dir += camera[2]
is_moving = true
if(Input.is_action_pressed("move_l")):
dir += -camera[0]
is_moving = true
if(Input.is_action_pressed("move_r")):
dir += camera[0]
is_moving = true
dir.y = 0
dir = dir.normalized()
velocity.y += delta * gravity
var hv = velocity
hv.y = 0
var new_pos = dir * SPEED
var accel = DE_ACCELERATION
if (dir.dot(hv)>0):
accel = ACCELERATION
hv = hv.linear_interpolate(new_pos, accel * delta)
velocity.x = hv.x
velocity.z = hv.z
velocity = move_and_slide(velocity, Vector3(0,1,0))
if is_moving:
var angle = atan2(hv.x, hv.z)
var char_rot = character.get_rotation()
char_rot.y = angle
character.set_rotation(char_rot)
Thanks for your help