1
\$\begingroup\$

I used delta to help with this but the object ended up rotation to much within a second.

extends KinematicBody2D
export var speed = 10.0


onready var sprite = $Sprite

var initpos = Vector2()
var dir = Vector2()
var velocity = Vector2()

#attack variables
var attacking = false
var max_attack_counter = 0.5
var attack_counter = max_attack_counter
var attack_speed = 200
var attack_rotation_speed = 1
var attack_rotation_distance = attack_rotation_speed * max_attack_counter
var attack_rotation = 0
var sword_init_x = 0
var sword_init_y = 0
var attack_type = 0
#Find way to get frames per second
var one_sec = 60



var counter = 0.0
var moving = false
var jumping = false
var col_inf = 0


class entity_input :

    var move_up = false
    var move_down = false
    var move_right = false
    var move_left = false
    var move_h = 0.0
    var move_v = 0.0
    var move_average = 0.0
    var clockwise = 0.0
    var counter_clockwise = 0.0
    var stance = 0
    var dodge = 0
    var sword_move_h = 0.0
    var sword_move_v = 0.0
    var sword_down = 0.0
    var sword_up = 0.0
    var sword_center = false
    var jump = false
    var aim
    var attack = false

var ei = entity_input.new()
enum direction { UP, DOWN, LEFT, RIGHT }
var facing = direction.DOWN

func _ready():


    initpos = position
    pass


#The z_index property wasn't working because the textue has multiple frames of sprites added to gether
#which made the texture 4 times as big as it thought it was
func _physics_process(delta):
    if(one_sec == 0):
        one_sec = 60





    get_input()
    use_input()
    set_dir()
    #Sword Stuff here

    sword(delta)




    move(delta)


    if facing == direction.DOWN:
        sprite.frame = 0
    elif facing == direction.UP:
        sprite.frame = 12
    elif facing == direction.LEFT:
        sprite.frame = 4
    elif facing == direction.RIGHT:
        sprite.frame = 8






#
func sword(delta):
    if($Sword):
        if($Sword and !attack(delta)):
            $Sword.look_at(ei.aim)



func set_dir(): #set moving
    dir = get_dir()

    if dir.x != 0 or dir.y != 0:

        if dir.x > 0:
            facing = direction.RIGHT
        elif dir.x < 0:
            facing = direction.LEFT
        elif dir.y < 0:
            facing = direction.UP
        elif dir.y > 0:
            facing = direction.DOWN

        moving = true
        initpos = position



func get_dir(): #user input
    velocity = Vector2()

    var v_input = (Vector2(ei.move_h, ei.move_v))
    var v_angle = v_input.angle()
    var v_length = 1
    #for game_pad
#   var v_length = v_input.length()
    if(ei.move_h != 0 or ei.move_v != 0):
        velocity += Vector2(speed * v_length, 0).rotated(v_angle)
        velocity.x = round(velocity.x)
        velocity.y = round(velocity.y)



    return velocity

func move(delta): # move the player linearly


    var collision = move_and_slide(velocity)
#add guided missle attack
#returns bool

func attack(delta):
    attack_type = 1

    match attack_type:
        0:
            return stab(delta)
        1:
            return swing(delta)

    return false

func stab(delta):
    if(!attacking and ei.attack):

        if($Sword):
            attack_counter = max_attack_counter
            attacking = true
            attack_rotation = $Sword.rotation
            if(is_in_group("Player")):
                $Sword.set_collision_mask_bit(2, true)


    if(attacking):
        var collision_info = $Sword.move_and_collide(Vector2(attack_speed, 0).rotated(attack_rotation + rotation) * delta)
        if(attack_counter > 0 and !collision_info):
                attack_counter -= delta


        else:
            attack_counter = 0
            attacking = false
            $Sword.position.x = sword_init_x
            $Sword.position.y = sword_init_y
            $Sword.set_collision_mask_bit(2, false)

        return true
    return false
#Must return bool
func swing(delta):
    #Start Attack
    if(!attacking and ei.attack):

        if($Sword):
            attack_counter = max_attack_counter
            attacking = true

            if(is_in_group("Player")):
                $Sword.set_collision_mask_bit(2, true)

    #When attacking
    if(attacking):
        $Sword.rotation += attack_rotation_speed * delta/one_sec
        var collision_info = $Sword.move_and_collide(Vector2())
        if(attack_counter > 0 and !collision_info):
                attack_counter -= delta


        else:
            attack_counter = 0
            attacking = false
            $Sword.position.x = sword_init_x
            $Sword.position.y = sword_init_y
            $Sword.set_collision_mask_bit(2, false)
        return true
    return false


func get_input():
    pass

func use_input():
#   if(ei.jump and !jumping):
#       jumping = false
#       set_collision_mask_bit(0, false)
#       set_collision_mask_bit(1, true)
#       set_collision_layer_bit(0, false)
    pass

Here is where I want to rotate the sword the amount that I set with the attack_rotation_speed but unless I divide delta by 60 it goes to fast. I don't like using magic constants but I don't know why it doesn't work.

    #When attacking
    if(attacking):
        $Sword.rotation += attack_rotation_speed * delta/one_sec
        var collision_info = $Sword.move_and_collide(Vector2())
        if(attack_counter > 0 and !collision_info):
                attack_counter -= delta
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Solved the problem, godot uses radians instead of degrees, that's what was causing my issues. So if in the code:

#When attacking
if(attacking):
    $Sword.rotation += attack_rotation_speed * delta/one_sec
    var collision_info = $Sword.move_and_collide(Vector2())
    if(attack_counter > 0 and !collision_info):
            attack_counter -= delta

I remove /onesec from it and for attack_rotation_speed I use radians instead of degrees.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .