0 votes

I want that, when I press m the camera to move around an Object but to keep looking at it.
Here are som pics: https://imgur.com/a/jU8Kj6a


extends KinematicBody

export var moveSpeed : float = 5.0
var vel : Vector3 = Vector3()

onready var camera = get_node("CameraOrbit")
onready var kaijuNodeAnim = get_node("CameraOrbit/Kaiju_Model_Anim2").get_node("AnimationPlayer")
onready var kaijuNodeModel = get_node("CameraOrbit/Kaiju_Model_Anim2")
onready var cameraOrbitNodeCamera = get_node("CameraOrbit").get_node("Camera")

func _physics_process(delta):
    vel.x = 0
    vel.x = 0

    var input = Vector3()
    if Input.is_action_pressed("ui_up"):
        input.x += 1
    elif Input.is_action_pressed("ui_down"):
        input.x -= 1
    elif Input.is_action_pressed("ui_right"):
        input.z += 1
    elif Input.is_action_pressed("ui_left"):
        input.z -= 1
    elif Input.is_action_pressed("ui_m"):
        cameraOrbitNodeCamera.translate(Vector3(cos(OS.get_ticks_msec()*0.01) * PI, 0, sin(OS.get_ticks_msec()*0.01)*PI))

    input = input.normalized()

    var dir = (transform.basis.z * input.z + transform.basis.x * input.x)

    vel.x = dir.x * moveSpeed
    vel.z = dir.z * moveSpeed

    vel = move_and_slide(vel, Vector3.UP)
Godot version 3.4.2
in Engine by (28 points)

1 Answer

+1 vote

var target = $target_object

$Camera.lookat(target.getglobal_transform().origin, Vector3.UP)

by (755 points)

Thanks! Works good. Now, for extra kudos, how can I make it that when I stop pressing "m" it returns to its original position?


var first_pos = null

func _ready():
    first_pos = cameraOrbitNodeCamera.global_transform.origin
    // first_pos = cameraOrbitNodeCamera.transform.origin


    cameraOrbitNodeCamera.transform.origin = first_pos
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.