How do I get a projectile to fire with a certain velocity? Here's the code it didn't work, found it online

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

extends KinematicBody2D

const SHOOT_VELOCITY = Vector2(800, -400)

var velocity = Vector2.ZERO

func _ready():

func launch(direction):
velocity = SHOOT_VELOCITY * Vector2(direction, 1)

func _physics_process(delta):
if Input.is_action_just_pressed(“attack”):
var Player = load(“res://Player/Player.tscn”)
var GreenBullet = load(“res://Bullets/GreenBullet.tscn”)
var greenBullet = GreenBullet.instance
greenBullet.x = Player.x
greenBullet.y = Player.y
var collision = move_and_collide(velocity * delta)
if collision != null:

:bust_in_silhouette: Reply From: SolenodonStudios

so im newer to the engine, but I can see what’s wrong.

greenbullet.x = player.x and greenbullet.y = player.y means that you’re teleporting the sprite over to the player, at least thats what I think.

what I would do (I dont know gdscript so well, I do python but you can probably figure out what I mean) is this:

if greenBullet.x > player.x:
greenBullet.x + 10 (or 20 or however many pixels you want)
elif greenBullet.x < player.x:
greenBullet.x - 10 (or 20 or however many pixels you want).
greenBullet.x = greenBullet.x

do the same thing but replace x with y, and maybe that might work. not trying it out in Godot myself, but yeah.

:bust_in_silhouette: Reply From: annemarietannengrund

im also not that far, but i watched a ton of videos the last days to get a feeling.

i remember this one from yesterday, he is showcasing and at one point talking about the projectile velocity.

this should give a an idea for a working approach i guess.

hope this helps in finding a solution :slight_smile: