How to modify the speed of my player?

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

Greets!

Here’s the script of my player/kinematicbody evoluing with mouse clicks on a map:

extends KinematicBody2D

var left_mouse_down = false
var tween_node = null
onready var global = get_node("/root/Global")

func _ready():
    tween_node = get_node("Tween") 
				if (global.player_map_position != null):
        self.global_position = global.player_map_position
				
func _physics_process(delta):
	if Input.is_mouse_button_pressed(1):
		if left_mouse_down == false:
			var mouse_pos = get_global_mouse_position()
			tween_node.interpolate_property(self, "global_position", global_position, mouse_pos, 1, Tween.TRANS_LINEAR, Tween.EASE_IN)
			tween_node.start()
		left_mouse_down = true
	else:
		left_mouse_down = false`

How could I modify its speed, depending on zones entered please?

:bust_in_silhouette: Reply From: Diet Estus

You can use Area2Ds for your zones, then link their body_entered signals to change the speed of your player.

In your particular setup, the “speed” of the player is determined by how fast you’re tweening from the player’s old position to his new position at the mouse click.

So, instead of interpolating your tween with a constant value of 1, I would instead use a variable called, for example travel_time. To start, you can set this value to 1.

var travel_time = 1

Then you can create Area2Ds on your map (with appropriate CollisionShape2D children) and set up their body_entered signal to change the travel_time variable. Smaller travel_time means faster player movement:

enter image description here

Then you can connect this signal to the player node and fill out the function. For example,

enter image description here

Yes, that’s exactly the way I wanna do it, but I don’t know the scripts for wich variables, and wich functions.

Syl | 2018-04-17 22:22

Instead of interpolating your position tweens using a constant value of 1, make this a variable called, for example, “travel_time”. Then set up Area2Ds on your map (with collision object children) and set up their on body entered signal to change the travel_time variable. A lower value means faster movement. Sorry I can’t be more specific at the moment (I’m on my phone).

Diet Estus | 2018-04-17 22:28

Ok, thxs for your time! Look, I’ve writen that script for my zones entered:

func _on_Area2D2_body_entered(body):
	get_node("/root/Global").speed = 22

That one on my singleton ‘Global’

var speed = null

And this one for my player:

var speed = get_parent().get_node("/root/Global").speed 
			var mouse_pos = get_global_mouse_position()
			tween_node.interpolate_property(self, "global_position", global_position, mouse_pos, speed, Tween.TRANS_LINEAR, Tween.EASE_IN)
			tween_node.start()
		left_mouse_down = true

That works almost fine. There’s a change of speed when entering the zones, but for the fact that upon entering it, there are few clicks where the player is brought backward, and then it works fine… How’s that please?
Ok, got it, it’s because it my player/sprite follows the first click in the zone at previous speed, and then the others moves it and bring it back on its first trajectory.
The change of speed happens only when my sprite reach the click in the zone, wherever that is. Wich is but a lil messy… A way to solve that?

And by the way, how to make the speed constant, and not exponential, wich is fast with far clicks, and slow with closer ones?

Syl | 2018-04-17 23:13

Before you interpolate, you can get the distance between the current position and the new position (that is, the mouse click position) and use this to modify the speed of your interpolation. With this, you can solve the exponential problem. Not sure about your other problems. I don’t use tweens to interpolate positions in my projects, so all this is new to me too.

Diet Estus | 2018-04-18 14:55

Ok, thxs a lot for your atention!

Syl | 2018-04-18 15:38