0 votes

Hi
I'm not sure if Godot has a built-in function for this or not, but I've tried to make something like the "lengthdir" function from GameMaker and although it works, it doesn't look as smooth and has some issues.

https://i.imgur.com/xRPUsqg.gif

As you can see, when I'm shooting in a straight line, it's fine, but as soon as I rotate the character (while shooting), it does a weird animation.

func shoot():
  if can_shoot:
    can_shoot = false
    var dir = Vector2(1, 0).rotated($Sprite/shootspot.global_rotation) 
    var kick = 200
    var kickdirection = (rad2deg(dir.angle())) - 180
    velocity.x = velocity.x + (kick * cos(kickdirection))
    velocity.y = velocity.y + (kick * -sin(kickdirection))
in Engine by (20 points)
edited by

1 Answer

0 votes

You should decrease the velocity in _process function.
I use a simple way to do it. That would decrease velocity rapidly.
velocity = velocity *0.7

by (300 points)

What does decreasing the velocity has anything to do with shoot kickback? I tried what you suggested but didn't make any difference.

I'm guessing it has something to do with these 2 lines??:

velocity.x = velocity.x + (kick * cos(kickdirection))
velocity.y = velocity.y + (kick * -sin(kickdirection))

Thanks.

What about something like this? Without cos and sin.
You need the opposite direction.

func shoot():

  if can_shoot:
    can_shoot = false
    var dir = Vector2(1, 0).rotated($Sprite/shootspot.global_rotation) 
    var kick = 200
    var kickdirection = kick * (dir*-1)
    velocity = velocity + kickdirection


func _process(delta):
  body.position += velocity
  velocity = velocity *0.7
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.