• 2D
  • my get_viewport().get_mouse_position() isn't working right

I'm trying to make a 2d platformer where you spawn an object and try to jump on it in the air before it falls.

the problem is when I try to spawn the tile it doesn't spawn where Is the cursor at it adds a relative value to the position that I don't know how to get rid of it.

you see when I try to instance a scene it takes the cursor position and viewport value into account but then something happens and I fount the object spawning way too far.

and this is the script I'm using, it's in the player1 scene

extends KinematicBody2D
# 
var score : int = 0
export var speed : int = 200
export var jumpforce : int = 600
export var gravity : int = 800


onready var AB1 = preload("res://player1AB.tscn")




var vel :Vector2 = Vector2()
onready var sprite : Sprite = get_node("sprite_idile")

onready var ui : Node = get_node("/root/mainscene1/CanvasLayer/ui")
onready var audioplayer : Node = get_node("/root/mainscene1/Camera2D/audio_player")
func _physics_process(delta):
	vel.x = 0
	# movement inputs
	if Input.is_action_pressed("move_left"):
		vel.x -= speed
	if Input.is_action_pressed("move_right"):
		vel.x += speed
	# applying the velcoty
	vel = move_and_slide(vel,Vector2.UP)

	#apllying gravty
	vel.y += gravity * delta

	#jump input
	if Input.is_action_just_pressed("jump") and is_on_floor():
		vel.y -= jumpforce




	# where the sprite facing
		if vel.x < 0:
		sprite.flip_h = true
	if vel.x > 0:
		sprite.flip_h = false
	if Input.is_action_just_pressed("restart"):
		death()



func death ():
	get_tree().reload_current_scene()

func collect_coin (value):

	score += value
	ui.set_score_text(score)
	audioplayer.play_coin_sfx()

func _input(event):
	if event.is_action_pressed("click"):
		var ABT1 = AB1.instance()
		add_child(ABT1)
		var XN = null
		XN = get_viewport().get_mouse_position()  

		ABT1.position = XN

important stuff

onready var AB1 = preload("res://player1AB.tscn")


func _input(event):
	if event.is_action_pressed("click"):
		var ABT1 = AB1.instance()
		add_child(ABT1)
		var XN = null
		XN = get_viewport().get_mouse_position()  

		ABT1.position = XN

sry first time doing this kind of stuff so I don't really know how to type it

Is AB1 in it's own scene at position 0,0? I only work in 3d, but if it's not at 0 position, it will throw it off. Don't know if it works that way in 2d or not.

@fire7side said: Is AB1 in it's own scene at position 0,0? I only work in 3d, but if it's not at 0 position, it will throw it off. Don't know if it works that way in 2d or not.

yep it's on 0,0

yeah it also works this way in 2d

Well, you could start out by printing the mouse position and the viewport size. See if those are correct. Click at upper left corner and bottom right corner and check the coordinates.

@fire7side said: Well, you could start out by printing the mouse position and the viewport size. See if those are correct. Click at upper left corner and bottom right corner and check the coordinates.

i hope i did this right

this is the modified script

	func _input(event):
		if event.is_action_pressed("click"):
			var ABT1 = AB1.instance()
	
			add_child(ABT1)
			var XN = null
			XN = get_viewport().get_mouse_position()  
			ABT1.position = XN
	
			print("this is the viewport = ",Viewport)
			print("this is the global mouse position = ",get_global_mouse_position())
			print("this is the ABT1 position = ",ABT1.position)

this is what is shown on the debugger

this is the viewport = [GDScriptNativeClass:691] this is the global mouse position = (-628, -268.991058) this is the ABT1 position = (116, 79)

this is the viewport = [GDScriptNativeClass:691] this is the global mouse position = (-329, -242.991058) this is the ABT1 position = (415, 105)

this is the viewport = [GDScriptNativeClass:691] this is the global mouse position = (-421, -88.991058) this is the ABT1 position = (323, 259)

this is the viewport = [GDScriptNativeClass:691] this is the global mouse position = (-214, -42.991058) this is the ABT1 position = (530, 305)

this is the viewport = [GDScriptNativeClass:691] this is the global mouse position = (-706, -295.991058) this is the ABT1 position = (38, 52)

I clicked 5 times a put the spaces to make reading easier


edit I added print("this is the maybe mouse position = ",get_viewport().get_mouse_position()) then click at the top left corner and this is what I got

this is the viewport = [GDScriptNativeClass:690] this is the global mouse position = (-734, -338.991058) this is the maybe mouse position = (10, 9) this is the ABT1 position = (10, 9)

then I changed the size of the window and clicked at almost the same positions

this is the viewport = [GDScriptNativeClass:690] this is the global mouse position = (-989, -440.491058) this is the maybe mouse position = (11, 8)

this is the ABT1 position = (11, 8)

edit 2 turns out that ABT1 is taking the same position as the mouse apparently

when you look at the debugger you see that they're in the same position

also if you take a look in abt1 in the remote inspector I also finds out that it's the same position what does that mean

Try using get_global_mouse_position in your code when setting the sprite and see if it's different. You are just using get_mouse_position now.

@fire7side said: Try using get_global_mouse_position in your code when setting the sprite and see if it's different. You are just using get_mouse_position now.

already tried that didn't work somehow made things worse in way I can't easily explain

Try printing the mouse position, not global, when the mouse is in the upper left corner and the lower right corner. Only those 2. Also, check in setup and see what your screen size is. Also print get_viewport().size.x, and size.y.

@fire7side said: Try printing the mouse position, not global, when the mouse is in the upper left corner and the lower right corner. Only those 2. Also, check in setup and see what your screen size is. Also print get_viewport().size.x, and size.y.

i don't know how to check in setup and see what your screen size is

I didn't know to put the X and Y on the same line so did the next good thing which is everyone is on it on line

#this is on the top left corner

this is the viewport = [GDScriptNativeClass:691] this is the viewport-X = 1024 this is the viewport-Y = 600

this is the global mouse position = (-743, -347.991058)

this is the mouse position = (1, 0)

this is the ABT1 position = (1, 0)

#this is on the bottom right corner

this is the viewport = [GDScriptNativeClass:691] this is the viewport-X = 1024 this is the viewport-Y = 600

this is the global mouse position = (278, 250.008942)

this is the mouse position = (1022, 598)

this is the ABT1 position = (1022, 598)

here I changed the size of the screen (made it a maximum size window)

this is on the top left corner

this is the viewport = [GDScriptNativeClass:691] this is the viewport-X = 1536 this is the viewport-Y = 801

this is the global mouse position = (-999, -445.491058)

this is the mouse position = (1, 3)

this is the ABT1 position = (1, 3)

this is on the bottom right corner

this is the viewport = [GDScriptNativeClass:691] this is the viewport-X = 1536 this is the viewport-Y = 801

this is the global mouse position = (532, 346.508942)

this is the mouse position = (1532, 795)

this is the ABT1 position = (1532, 795)

So, the mouse position and sprite position are the same, but it isn't where it's supposed to be, right? Another thing to try is just start a new project. Use the godot sprite and don't even make it another scene. Then use the get_mouse position and set_mouse_position to see if your code or setup is messing it up. Then make the sprite a separate scene in the same way and see if it works or not. Also try get_global_position mouse and set_global_position for sprite. You should see a blue frame of your window if you zoom out enough. the 0,0 is the upper left corner of the frame.

This sounds related to https://github.com/godotengine/godot/issues/29570.

If you're using the 2d or viewport stretch mode, your mouse position will not match the one reported by the viewport. To work around this, you have to divide or multiply this coordinate by the automatically determined scale factor. Here's an example: https://github.com/godotengine/godot-demo-projects/blob/89978a74211579629349b058a8799859d5d98d13/3d/waypoints/waypoint.gd#L41-L63

https://stackoverflow.com/a/68538164/16529873

someone answered me there and helped me In a way that would make it easy to port the game to mobile so I went with his way. but he also mentions the stretch mode as well but I really have no idea what that means so I need to look into that. but thank you guys a lot in the worst case you helped me know how to debug my game and look for what is the problem. but you showed me how great is this community cuz this is my first time doing it but I'm really impressed I thought I would need to pay someone to help me with that like a coach or something so thank you all again

@Calinou @fire7side

Glad you got something working, or at least a direction to go in.

but he also mentions the stretch mode as well but I really have no idea what that means so I need to look into that.

Stretch modes are explained in the Multiple resolutions documentation.