0 votes

I have 2 scenes in my project, my main scene and my bullet scene.

Here is my code for the player:

extends KinematicBody2D

onready var BULLET = preload("res://bullet.tscn")
onready var BULLET_SPAWN = $bullet_spawn
onready var BULLET_SFX = preload("res://9mm-pistol-shoot-short-reverb.wav")

var direction = Vector2(0,0)
var bullet_direction = Vector2(1,0)
var bullet_rotation = 0

const DEADZONE = 0.5

export var walk_speed = 200
export var jump_speed = -400
export var gravity = 200

var spawn_position = Vector2()

func get_input():
    direction.x = 0

    var aim_input = Vector2(0,0)

var left = Input.is_action_pressed("left")
var right = Input.is_action_pressed("right")
var jump = Input.is_action_just_pressed("jump")
var shoot = Input.is_action_just_pressed("shoot")

var aim_left = Input.is_action_pressed("aim_left")
var aim_right = Input.is_action_pressed("aim_right")

var aim_up = Input.is_action_pressed("aim_up")
var aim_down = Input.is_action_pressed("aim_down")

if left:
    direction.x -= walk_speed
if right:
    direction.x += walk_speed
if jump and is_on_floor():
    direction.y = jump_speed

if shoot:

aim_input.x = int(aim_left) - int(aim_right)
aim_input.y = int(aim_up) - int(aim_down)

if aim_input.length() > DEADZONE:
    var aim_angle_in_degrees = rad2deg(aim_input.angle())

    var input_aim_dir = int(round(aim_angle_in_degrees/45)* 45)

    var aim_direction

    match input_aim_dir:
            aim_direction = Vector2(1,0)
            #print("aim left")
            spawn_position = $left.position
            bullet_direction = Vector2(-1,0)
            bullet_rotation = 0
            aim_direction = Vector2(1,-1).normalized()
            #print("aim left down")
            spawn_position = $left_down.position
            bullet_direction = Vector2(-1,1)
            bullet_rotation = 225
            aim_direction = Vector2(0,-1)
            #print("aim down")
            spawn_position = $down.position
            bullet_direction = Vector2(0,1)
            bullet_rotation = 270
            aim_direction = Vector2(-1,-1).normalized()
            #print("aim right down")
            spawn_position = $right_down.position
            bullet_direction = Vector2(1,1)
            bullet_rotation = -45
        -180, 180:
            aim_direction = Vector2(-1,0)
            #print("aim right")
            spawn_position = $right.position
            bullet_direction = Vector2(1,0)
            bullet_rotation = 0
            aim_direction = Vector2(-1,1).normalized()
            #print("aim right up")
            spawn_position = $right_up.position
            bullet_direction = Vector2(1,-1)
            bullet_rotation = 45
            aim_direction = Vector2(0,1)
            #print("aim up")
            spawn_position = $up.position
            bullet_direction = Vector2(0,-1)
            bullet_rotation = 90
            aim_direction = Vector2(1,1)
            #print("aim left up")
            spawn_position = $left_up.position
            bullet_direction = Vector2(-1,-1)
            bullet_rotation = 135

func _physics_process(delta):


    direction.y += gravity * delta

    $bullet_spawn.position = spawn_position

    var movement = move_and_slide(direction, Vector2(0,-1))

func shoot():
    var bullet = BULLET.instance()
    bullet.direction = bullet_direction

    bullet.global_position = BULLET_SPAWN.global_position

( the spawn position variable is accessing 4 position 2Ds set where i want the bullet to spawn from.)

this is my bullet code.

extends Area2D

var speed = 15
var direction = Vector2()

func _physics_process(delta):
    position += direction * speed * delta 

func rotate_bullet(var degrees):
    rotation = degrees

my bullet moves in the correct direction but it never rotates correctly except when I am shooting left or right and the rotation is set to zero. It does rotate, but never in a correct way. I have no idea what I'm doing wrong, In my bullet scene I tried all these rotation amounts and they worked fine, just not when i set them in code. I have tried using rotation = rad2deg(degrees) instead of rotation = degrees but it didn't fix it.

Thanks. :-)

in Engine by (389 points)

1 Answer

+1 vote
Best answer

I'd guess your assumptions of it being a radians vs degrees issue is correct. Your angles are being set as degrees, but the rotation property of your bullet expects radians.

Your attempted fix of calling rad2deg() won't work as that converts radians to degrees, not degress to radians. You would need instead to call deg2rad().

However, instead of setting the bullet's rotation property (which expects radians), set its rotation_degrees property, which expects degrees.

by (14,952 points)
edited by

thanks, it worked! :D

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 Frequently asked questions and 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 [email protected] with your username.