|
|
|
|
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.
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.
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.
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!
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.
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.
Millard | 2020-10-31 15:37