daz-b-like / ProCam2D_Godot4.x

The ultimate camera for all your 2D needs.
MIT License
53 stars 2 forks source link

PCamMouseFollow does not work! #9

Open szabiszekely opened 1 month ago

szabiszekely commented 1 month ago

Describe the bug Cannot add Mouse follow without crashing To Reproduce Add a PCamMouseFollow addon to a ProCam2D

Screenshots image_2024-09-30_135306800

Desktop (please complete the following information):

allendawodu commented 5 days ago

Instead of dividing by 2, divide by 2.0.

Here's an improved version of the script that includes damping (smoothing):

@tool
extends PCamAddon
class_name PCamMouseFollow

@export var max_offset := Vector2(300.0, 300.0)
@export_range(0, 1) var damping := 0.1
var actual_offset := Vector2.ZERO

func _init():
    stage = "pre_process"

func pre_process(camera, delta):
    if not enabled or camera._playing_cinematic:
        return

    var viewport = camera.get_viewport()
    var mouse_position = viewport.get_mouse_position() - viewport.size / 2.0
    var viewport_size = viewport.size / 2

    var desired_offset = Vector2(
        clamp(mouse_position.x / viewport_size.x, -1, 1) * max_offset.x,
        clamp(mouse_position.y / viewport_size.y, -1, 1) * max_offset.y
    )
    actual_offset = actual_offset.lerp(desired_offset, damping)

    # Add the calculated offset to the existing camera position
    camera._target_position += actual_offset