My bullet isn't rotation correctly.

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

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:
	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:
		0:
			aim_direction = Vector2(1,0)
			#print("aim left")
			spawn_position = $left.position
			bullet_direction = Vector2(-1,0)
			bullet_rotation = 0
		-45:
			aim_direction = Vector2(1,-1).normalized()
			#print("aim left down")
			spawn_position = $left_down.position
			bullet_direction = Vector2(-1,1)
			bullet_rotation = 225
		-90:
			aim_direction = Vector2(0,-1)
			#print("aim down")
			spawn_position = $down.position
			bullet_direction = Vector2(0,1)
			bullet_rotation = 270
		-135:
			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
		135:
			aim_direction = Vector2(-1,1).normalized()
			#print("aim right up")
			spawn_position = $right_up.position
			bullet_direction = Vector2(1,-1)
			bullet_rotation = 45
		90:
			aim_direction = Vector2(0,1)
			#print("aim up")
			spawn_position = $up.position
			bullet_direction = Vector2(0,-1)
			bullet_rotation = 90
		45:
			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):

    get_input()

    direction.y += gravity * delta

    $bullet_spawn.position = spawn_position


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

func shoot():
    Input.start_joy_vibration(0,.7,.75,.1)
    $sfx.play()
    var bullet = BULLET.instance()
    get_parent().add_child(bullet)
    bullet.rotate_bullet(bullet_rotation)
    bullet.direction = bullet_direction
    print(bullet.rotation)

    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. :slight_smile:

:bust_in_silhouette: Reply From: jgodfrey

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.

thanks, it worked! :smiley:

Millard | 2020-10-01 22:15