Hello.
In my 2D game, while shooting, the cursor position is changed after each shot by the following method:
func get_resolution_ratio():
return get_viewport().size.x / 800 # Game resolution 800x600
func shot():
# Part of the method:
Input.warp_mouse_position(get_viewport().get_mouse_position() * get_resolution_ratio() + Vector2(rand_range(-10 * get_resolution_ratio() * gun_power, 10 * get_resolution_ratio() * gun_power), rand_range(-10 * get_resolution_ratio() * gun_power, 10 * get_resolution_ratio() * gun_power)))
The get_resolution_ratio()
method is needed because get_viewport().get_mouse_position()
returns the mouse position using the game resolution set in the project settings, while Input.warp_mouse_position()
uses the resolution that actually exists at the moment. Because of this, you need to multiply the argument by the ratio of the resolution from the settings and the really existing resolution.
The problem is that it doesn't work correctly, in full screen mode the cursor always moves up and with a much larger step relative to windowed mode (taking into account the difference in resolutions).
Project sources. Used code.
What did I do wrong? Why doesn't it work? Or do you need to decide differently?