![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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