Mouse in KinematicBody2D

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

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):

self.position.x-= 1
self.position.x+= 1
self.position.y-= 1
self.position.y+= 1

#Quit on Q press

#Check for mouse movement
self.position = get_viewport().get_mouse_position()

:bust_in_silhouette: Reply From: Thomas Karcher

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 :slight_smile:

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

KinematicBody2D mouse movement script

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 :slight_smile: tyvm for your help

Sanctumm | 2019-09-09 12:39