How can i make the player move by swing?

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

The control of my game should be holding left click to display circles that, depending on how far of the character it’s the mouse position, it would appear just one circle or a row of circles one bigger than the other (to mark the strengh to which the player will speed burst) and when released, the char applies that force, bounce if there’s a collision while being in middle of that impulse and then stops smoothly.
I.e: like a slingshot.

any reason you’re using a kinematic body vs a rigid body? a rigid body might be easier to slingshot but if its not what your going for thats totally fine.

Millard | 2020-10-29 15:30

Hi! Sorry, i confused rigid body with move and collide(), i’ll fix that.
My game it’s 2D.

Gonz4 L | 2020-10-29 15:39

:bust_in_silhouette: Reply From: Millard

Here’s my code.

extends RigidBody2D

var speed = 1

func _physics_process(delta):
var mouse_position = get_global_mouse_position()

var sling_direction = mouse_position - self.position 

if Input.is_action_just_released("slingshot"):
	set_linear_velocity(sling_direction * speed)

While this doesn’t have rings which change the speed, it does have more speed depending on how far the mouse is from the player. Hope this helps!

oh, and speed is set to 1 because I assumed I would need more speed but it turned out I didn’t.

Millard | 2020-10-29 23:23

oh, did some more testing. If you allow the player to slingshot again before he touches ground, it will get faster and faster. I’m sure there’s a way to fix this. :slight_smile:

Millard | 2020-10-29 23:28

I did it with apply central impulse instead of linear velocity but my problem now it’s that the character should stop smoothly (reducing it’s velocity gradually to 0) when left click is pressed or hold because, if not, it’s very uncontrolable.

Gonz4 L | 2020-10-30 13:26

I’m not an expert with rigid bodies, but maybe divide the velocity by a varible every frame while left click is down?

Millard | 2020-10-30 15:24

well, just tried it and now I doubt it will work. :frowning:

Millard | 2020-10-30 15:32

Got it almost!

if Input.is_action_just_pressed("Slow"):
	apply_central_impulse(-linear_velocity * 2)

the problem is that this doesn’t slow it to zero, so it still needs some work, but I think its the right idea.

Millard | 2020-10-30 15:36

Got It!

func _integrate_forces(state):
    if Input.is_action_pressed("Slow"):
	    var speed = state.linear_velocity.length()
	    speed -= deceleration
	    if speed < 0:
		    speed = 0
	    state.linear_velocity = state.linear_velocity.normalized() * speed

actually I got both the things I suggested from another question on this site when i did a google search so I didn’t come up with it myself. :slight_smile:

Hope it works!

Millard | 2020-10-30 15:54

Thanks! Just would had been good to spawn an arrow that strecth more or less depending on how far the mouse position it’s but mechanic’s the most important part so thank you again!

Gonz4 L | 2020-10-31 01:55

just find the distance between the player and the mouse position and multiply it times your speed variable. should do the trick! :slight_smile:

Millard | 2020-10-31 03:07

oh, didn’t read your comment carefully enough. Thought you were saying you wanted it to slow faster depending on your mouse position like you did with your movement. :slight_smile:

Unfortunately If I was trying to make that type of arrow, I would probably end up asking on this site, so it wouldn’t help you much. lol.

Millard | 2020-10-31 03:32

Doesn’t matter so much. Thanks for your accidental advice in how to slow the player faster depending in it’s velocity! XD

Gonz4 L | 2020-10-31 14:01

np. Actually when I look back, you should probably multiply the distance by the deceleration value. I got the variable name wrong. :slight_smile:

Millard | 2020-10-31 15:37