stop event.get_relative() being added to camera2D when limit reached

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Coenster

Hello.

I am moving a camera2D using ScreenDrag like this:

func _input(event):
    if event is InputEventScreenDrag:
        MyCam.move_local_x(event.get_relative().x)
        MyCam.move_local_y(event.get_relative().y)

The camera is moving perfectly and it stops perfectly according to the limits I set.

The problem is that if I continue to drag across the screen after the camera reached the limit, the camera is not moving like it should, but somehow the event.get_relative is still getting added to the camera position.

This means that when the camera reaches limit_left and I drag left 3 more times, I have to drag the camera right 3 times and on the fourth drag it only starts to move right. It is like it is undoing the extra 3 left drags first.

Can you please show me how to avoid this. How can I stop event.get_relative() being added when the camera reaches its limits. I tried using something like clamp but i got the same results.

Thanks in advance

Hey, in your code snippet, what is MyCam and what node is the script attached to? Is MyCam an instance of Camera2D, or of a parent Node2D? My answer assumes that “MyCam” is a custom camera implementation, but after taking another look at the docs I’m not sure whether I was correct to assume that…

jhigginbotham64 | 2019-10-18 04:19

:bust_in_silhouette: Reply From: jhigginbotham64

If my understanding of InputEventScreenDrag.relative is correct, then without knowing how your MyCam works internally I would guess that the following changes might solve your issue:

func _input(event):
    if event is InputEventScreenDrag:
        # both move_local calls moved to their own if-blocks
        if MyCam.can_move_local_x(event): 
            MyCam.move_local_x(event.get_relative().x)
        if MyCam.can_move_local_y(event): 
            MyCam.move_local_y(event.get_relative().y)

…where can_move_local_x and can_move_local_y are functions you add to MyCam, which check event.get_relative() against your internal camera state and returns true if would be able to correctly perform the update along the given axis.