# Mouse in KinematicBody2D

Attention Topic was automatically imported from the old Question2Answer platform.

Heya guys. I am totally new to coding but want to get started in it. Ive seen alot of tutorials lately and want to work on a basic Tile style game where i can just roam aorund.

My question is - How do i get mouse to work for normal click and move

i get get it to work when working on a sprite node but it just summons there asap id like to get a click and walk/run to clicked area.

Cant get any mouse click to work in KinematicBody2d even tho im following the basic tutorials

extends KinematicBody2D

export (int) var speed = 200

var target = Vector2()
var velocity = Vector2()

func _input(event):
if event.is_action_pressed(‘click’):
target = get_global_mouse_position()

func _physics_process(delta):
velocity = (target - position).normalized() * speed
# rotation = velocity.angle()
if (target - position).length() > 5:
velocity = move_and_slide(velocity)

!Above doesnt work… Any idears?

Buttom one is what i got to work for now but its for a sprite :

extends Sprite

func _process(delta):

if(Input.is_key_pressed(KEY_A)):
self.position.x-= 1
if(Input.is_key_pressed(KEY_D)):
self.position.x+= 1
if(Input.is_key_pressed(KEY_W)):
self.position.y-= 1
if(Input.is_key_pressed(KEY_S)):
self.position.y+= 1

#Quit on Q press
if(Input.is_key_pressed(KEY_Q)):
get_tree().quit()

#Check for mouse movement
if(Input.is_mouse_button_pressed(BUTTON_LEFT)):
self.position = get_viewport().get_mouse_position()

It’s always a good idea to check the debugger output first. In your case, it says:

“ERROR: Request for nonexistent InputMap action: click”

Which is correct, since in the default project settings, there’s no mapping for a “click” action. The best way to handle mouse clicks in Godot 3 and saving the mouse position is:

``````func _input(event):
if event is InputEventMouseButton:
target = event.position
``````

After this change, the player moves as expected.

How would you write the script? tried to implent it but still doesnt work… Can you please write the full script so i can just implent it and try it out

Sanctumm | 2019-09-09 11:41

Just replace your _input function in your first code example with the one I posted above and leave the rest of the code (the variable definitions and the _physics_process function) as it is.

Thomas Karcher | 2019-09-09 11:54

Sk-rmbillede-427 hosted at ImgBB — ImgBB

Tried to implent it but nothing happens…

Uploaded a picture of the script hope you can point out what is wrong

Sanctumm | 2019-09-09 12:15

Somehow the underscores got lost both in the _input and in the _physics_process function. Once you add those again, everything should work.

Thomas Karcher | 2019-09-09 12:26

Sk-rmbillede-429 hosted at ImgBB — ImgBB

Works perfectly tyvm for your help

Sanctumm | 2019-09-09 12:39