my get_viewport().get_mouse_position() isn't working right

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

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

the 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

this the same problem in Godot form check it out if possible in case someone answered there

https://godotforums.org/discussion/27007/my-get-viewport-get-mouse-position-isnt-working-right#latest

Guess you need to use TileMap.map_to_world and its counterpart Node.to_globalenter link description here

clemens.tolboom | 2021-07-26 19:50

:bust_in_silhouette: Reply From: mhmoodalem

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