![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | djskidds |
Im creating a 2d game using keyboard for all buttons. When i press the shoot button the bullet always goes to the right. I have been able to get it to spawn from other directions but it still faces and moves to the right. How do i get the bullet to rotate and move in the direction the player is facing (ie. facing up bullet goes up). Below is my player code. Bullet code is near the bottom. Thank you in advance.
extends KinematicBody2D
const ACCELERATION = 500
const MAX_SPEED = 80
const FRICTION = 500
enum {
MOVE,
ATTACK
}
var can_fire = true
var state = MOVE
var velocity = Vector2.ZERO
onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree
onready var animationState = animationTree.get(“parameters/playback”)
const ACID = preload(“acid.tscn”)
func _ready():
animationTree.active = true
func _process(delta):
match state:
MOVE:
move_state(delta)
ATTACK:
attack_state(delta)
func move_state(delta):
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength(“move_right”) - Input.get_action_strength(“move_left”)
if Input.is_action_just_pressed(“move_left”):
$anchor.rotation_degrees = 180
if Input.is_action_just_pressed(“move_right”):
$anchor.rotation_degrees = 0
input_vector.y = Input.get_action_strength(“move_back”) - Input.get_action_strength(“move_forward”)
if Input.is_action_just_pressed(“move_back”):
$anchor.rotation_degrees = 90
if Input.is_action_just_pressed(“move_forward”):
$anchor.rotation_degrees = 270
input_vector = input_vector.normalized()
if input_vector != Vector2.ZERO:
animationTree.set("parameters/Idle/blend_position", input_vector)
animationTree.set("parameters/Walk/blend_position", input_vector)
animationTree.set("parameters/Attack/blend_position", input_vector)
animationState.travel("Walk")
velocity = velocity.move_toward(input_vector * MAX_SPEED, ACCELERATION * delta)
else:
animationState.travel("Idle")
velocity = velocity.move_toward(Vector2.ZERO, FRICTION * delta)
velocity = move_and_slide(velocity)
if Input.is_action_just_pressed("acid throw"):
state = ATTACK
var acid = ACID.instance()
acid.position = $anchor/Position2D.global_position
acid.position = $anchor/Position2D.global_position
get_parent().add_child(acid)
func attack_state(_delta):
velocity = Vector2.ZERO
animationState.travel(“Attack”)
func attack_animation_finished():
state = MOVE
** Here is the bullet code:
extends Area2D
var speed= 200
func _physics_process(delta):
position += transform.x * delta * speed
func _on_VisibilityNotifier2D_screen_exited():
queue_free()