Projectile doesn't change direction with my character (as a KinematicBody2D)

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

Hey all, hope everyone is doing well. I’ve been having an issue with my “bullet” projectile not changing direction along with my character. I’m using the KidsCanCode tutorial on projectiles found here: http://kidscancode.org/godot_recipes/2d/2d_shooting/

The projectile behaves properly other than the direction. I’m pretty sure it’s because my Position2D node doesn’t change direction as movement is applied and I’m not sure how to code that.

Code on the Kinematic Body 2D

extends KinematicBody2D

var WALK_SPEED = 5600
var FRICTION = 600

var velocity = Vector2.ZERO

export (PackedScene) var Bullet

onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get("parameters/playback")

func shoot():
	var b = Bullet.instance()
	owner.add_child(b)
	b.transform = $Position2D.global_transform

func get_input():
	velocity = Vector2.ZERO
	if Input.is_action_pressed("right"):
		velocity.x += 1
	if Input.is_action_pressed("left"):
		velocity.x -= 1
	if Input.is_action_pressed("down"):
		velocity.y += 1
	if Input.is_action_pressed("up"):
		velocity.y -= 1
	velocity = velocity.normalized()
	
	if velocity != Vector2.ZERO:
		animationTree.set("parameters/Idle/blend_position", velocity)
		animationTree.set("parameters/Walk/blend_position", velocity)
		animationState.travel("Walk")
		velocity += velocity * WALK_SPEED
	else:
		animationState.travel("Idle")
		velocity = velocity.move_toward(Vector2.ZERO, FRICTION)

	if Input.is_action_just_pressed("shoot"):
		shoot()


func _physics_process(delta: float) -> void:
	get_input()
	velocity = move_and_slide(velocity * delta)

Code for my bullet

extends Area2D

var speed = 750

func _physics_process(delta):
	position += transform.x * speed * delta

func _on_Bullet_body_entered(body: Node) -> void:
	queue_free()

Any help would be much appreciated. Cheers!

The move_and_slide(velocity * delta) function you shouldn’t multiply with delta, the function already did it for you and that’s why your move speed is so high. Docs

And you shouldn’t be using ALL CAPS letters for normal variables, that’s used for constants
const WALK_SPEED

Multirious | 2021-07-05 08:47

:bust_in_silhouette: Reply From: KaiProton

Hey,
I had the exact same issue, and after googling up the universe, well, youtube actually, I made my own changes,

I changed the Bullet Script to this:

    extends Area2D

var speed = 100
var velo = Vector2()

func _physics_process(delta: float) -> void:
	velo.x = speed *delta
	translate(velo)

func _on_VisibilityNotifier2D_screen_exited() -> void:
	queue_free()

and in the shooting Section of the player;
I set the sprite to an variable

onready var anim = get_node("Anims")

so then when the direction of the player was flipped the bullet could reference it when called, and adjust the speed,

func shoot():
	var b = shot.instance()
	
	if !anim.flip_h:
		b.speed = 200
	elif anim.flip_h:
		b.speed = -200
		
	get_parent().add_child(b)
	b.position = $ShotSpawn.global_position

of course the issue that remains, is I had to put the Position2d
($ShotSpawn in this case) dead centre of my characters gun, so it looks like it shoots from the same place each time,

:bust_in_silhouette: Reply From: Multirious

So basically, the player node is always facing in the same direction, you have to make the player look at the mouse, and the code is very simple since Godot already provided a built-in function for that.

look_at(get_viewport().get_mouse_position())

Just put this code in the player’s node process function.