# Normalizin the walk.

Attention Topic was automatically imported from the old Question2Answer platform.

Greets!

Here’s my script to make my player/sprite move in my map:

``````    Extends Node2D

set_process(true)

func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
\$Tween.interpolate_property(\$Player/Sprite, "global_position",\$Player/Sprite.global_position, get_global_mouse_position(), 1,Tween.TRANS_LINEAR,Tween.EASE_IN)
\$Tween.start()
``````

Trouble is, if far away click, the sprite rush to reach it, and I want it with a slow, regular motion. Other trouble, movin by mouse click leaves the sprite/player undetected in my arrea2Ds.

Any help folks?

You can calculate the distance from the player to the mouse click position by doing some simply math like player.position - get_global_mouse_position(). This value must be positive so you can use abs() function, now divide this value to get good time of tweening, just try to fit the best. This way makes your movement will have always the same speed

Thxs for your reply, but I rather inexperimented in script… Where do I put that simple math? And what’s the abs() func please? And, actually I’d like my movement bein modified by the diferent zones…

Syl | 2018-03-27 18:05

You put it under you tween code, on left button click. abs() returns absolute value of number so return value of abs(-10) and abs(10) will be always positive, so it’s just 10. You need it because tween duration must be positive. I don’t fully understand what is: being modifed by the different zones, but if it’s just speed, you can make a variable multiplier which will change when node enters the zone. Then just multiply your result of calculating absolute value of distance from player to mouse click by variable multiplier, save it in e.g variable speed, and in tween put in tween duration and thats it.

lulu | 2018-03-27 18:52

I don’t know where and how to put it, sry. Could you write it?
And yes, it’s changes acordin to zones entered (forest, river…).

Syl | 2018-03-27 19:31

it will be something like that:

``````var terrain_speed = 1.0 #speed of movement in current zone, you have to make code which will change this value when player enters to zone

var speed = 100 #more = faster, less = slower, try to fit the best

func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
#helpers
var player_pos = \$Player/Sprite.global_position
var mouse_pos = get_global_mouse_position()

var duration = (abs(player_pos - mouse_pos) * terrain_speed)/speed

\$Tween.interpolate_property(\$Player/Sprite, "global_position", player_pos, mouse_pos, duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
\$Tween.start()
``````

lulu | 2018-03-27 21:59

Crash: Invalid built in funciton ‘abs’ . Cannot convert argument 1 from nil to float…

Syl | 2018-03-27 22:14

oops, there is one ) to remove from variable mouse_pos at the end, this should help

lulu | 2018-03-27 22:22

Nops, same issue.

Syl | 2018-03-27 22:24

Wait, put the script on node2D root, maybe wrong?

Syl | 2018-03-27 22:28

This one should be ok.

``````   var terrain_speed = 1.0 #speed of movement in current zone, you have to make code which will change this value when player enters to zone

var speed = 500 #more = faster, less = slower, try to fit the best

func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
#helpers
var player_pos = \$Player/Sprite.global_position
var mouse_pos = get_global_mouse_position()

var distance = player_pos.distance_to(mouse_pos)
var duration = distance * terrain_speed/speed

\$Tween.interpolate_property(\$Player/Sprite, "global_position", player_pos, mouse_pos, duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
\$Tween.start()
``````

lulu | 2018-03-27 22:39

I would see your tree architecture to help

lulu | 2018-03-27 22:41

Syl | 2018-03-27 22:50

Hmm, it looks good, there is any error with new code?

lulu | 2018-03-27 22:55

Here we are! Big up!
Though, now I still have the trouble of my player not bein detected by my area2Ds and camera when moved by mouse.

Syl | 2018-03-27 23:20