0 votes

I have a spatial node with a mesh and a camera attached to a KinematicBody:

KinematicBody
---spatial
------ mesh
---Camera

I can rotate the camera with the mouse. However, I want the camera to slowly rotate back to its initial position (angle zero) when I stop moving the mouse. I have tried many options without success.

One of the options is to use the "lerp" function to restore the position of the camera if the mouse is not moving, but I don't know how to do this. Please Help.
Here is the code

extends Spatial
export var sensitivity = 0.4
export var min_angle = -90
export var max_angle = 90
export var distance = 5.0
var _rotation_w_right_left = 0
var _rotation_w_up_down = 0
var motion = Vector2()
func _ready():
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
var motion = Vector2()
func _process(delta):
    if abs(motion.length())<0.01:
        _rotation_w_right_left = lerp(_rotation_w_right_left , -120, 120)
        _rotation_w_up_down = lerp(_rotation_w_up_down , -70, 70)
        update_rotations()
func _input(event):
        if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED :
            # Get mouse delta
            motion = event.relative
            # Add to rotations
            _rotation_w_right_left -= motion.x * sensitivity
            _rotation_w_up_down += motion.y * sensitivity
            # Clamp pitch
            var e = 0.001
            if _rotation_w_up_down > max_angle-e:
                _rotation_w_up_down = max_angle-e
            elif _rotation_w_up_down < min_angle+e:
                _rotation_w_up_down = min_angle+e
            _rotation_w_up_down = clamp(_rotation_w_up_down, -70, 70)
            _rotation_w_right_left = clamp(_rotation_w_right_left, -120, 120)
            # Apply rotations
            update_rotations()
func update_rotations():
    set_rotation(Vector3(0, deg2rad(_rotation_w_right_left), 0)) 
    rotate(get_transform().basis.x.normalized(), -deg2rad(_rotation_w_up_down))
in Engine by (129 points)

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 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 webmaster@godotengine.org with your username.