how to make my object stay in position?

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

hello everyone, can some tell me how to make my object position stay in red circle at start game? because every time i start game my object position at 0,0

this my player scene script

func _process(delta: float) -> void:
global_position = lerp(global_position,get_viewport().get_mouse_position(),0.2)

i try do something like this,

var can_move_mouse = false

func _ready() -> void:
	global_position = Vector2(145,475)

func _process(delta: float) -> void:
	if can_move_mouse == true:	
		global_position = lerp(global_position,get_viewport().get_mouse_position(),0.2)

func _on_Timer_timeout() -> void:
	can_move_mouse = true

here i record the problem
but when time out and mouse not inside screen, my position go back to 0,0. i want it stay at it position if mouse not in screen

Please show all the code about this issue.

ATom 1 | 2020-01-09 09:59

Because when you run the game, the mouse has not entered the window, so get_mouse_position () will return 0,0.

ATom 1 | 2020-01-09 11:52

Yes, I realized that the reason is very simple, please hide these wrong ideas and prevent the wrong guidance of users with other similar problems

ATom 1 | 2020-01-09 12:14

:bust_in_silhouette: Reply From: ATom 1

Try this

func _input(event):
      if can_move_mouse ==true and event is InputEventMouseMotion:
           global_position = lerp(global_position,get_viewport().get_mouse_position(),0.2)

If it affects other input events, you can change it to

func _unhandled_input(event):

ATom 1 | 2020-01-09 12:08

thank allot your first code work, but i dont understand why it work thought, can you tell me why?

potatobanana | 2020-01-09 12:13

There is an internal variable in the engine that stores the current mouse position. Get_mouse_position () will return this value, but if your mouse never enters the window, this variable is always (0,0),In other words: this internal variable will only be updated when the mouse is inside the window

ATom 1 | 2020-01-09 12:18

thank you ,you really help me.

potatobanana | 2020-01-09 12:23