I have a problem, I’m making a 2d top down shooter and I’m making dodgeroll. When I roll I go through the walls of my map, how to fix this?
extends CharacterBody2D
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@onready var deg_timer = $DegTimer
@onready var weapons = $Weapons
@onready var health_component = $HealthComponent
@onready var roll_timer = $RollTimer
@onready var switch_cooldown = $SwitchCooldown
@export var roll_distance: float = 180
@export var roll_duration: float = 1
var velocity_backup: Vector2 = Vector2.ZERO
var is_rolling: bool = false
var roll_direction: Vector2 = Vector2.ZERO
var current_animation = "idle"
const speed = 150
var a = 0
var saved_collision_layer
var saved_collision_mask
var nearby_weapon: Area2D = null
var current_weapon: Node2D = null
var inventory: Array = []
var current_weapon_index: int = 0
var dves = 270
var dev = 90
var switch_buffer = 40
func _ready():
animated_sprite_2d.play("idle0")
add_to_group("player")
func _process(delta):
current_animation = "idle"
var mouse = get_local_mouse_position()
a = snapped(mouse.angle(), PI/4 / (PI/4))
a = wrapi(int(a), 0, 8)
if movement_vector() != Vector2(0, 0) and roll_timer.is_stopped():
current_animation = "run"
animated_sprite_2d.play(current_animation + str(a))
if movement_vector() == Vector2(0, 0) and roll_timer.is_stopped():
current_animation = "idle"
animated_sprite_2d.play(current_animation + str(a))
var movement = movement_vector()
var direction = movement.normalized()
velocity = speed * direction
move_and_slide()
if Input.is_action_just_pressed("interact") and nearby_weapon:
pick_up_weapon(nearby_weapon)
if switch_cooldown.is_stopped():
if Input.is_action_just_pressed("quick_switch"):
quick_switch_weapon()
if Input.is_action_just_pressed("next_weapon"):
next_switch_weapon()
if Input.is_action_just_pressed("prev_weapon"):
previous_switch_weapon()
if current_weapon:
if current_weapon.rotation_degrees >=0:
current_weapon.z_index = 2
if current_weapon.rotation_degrees >=180:
current_weapon.z_index = 0
if Input.is_action_just_pressed("right_click") and not is_rolling and not movement_vector() == Vector2(0, 0):
saved_collision_layer = collision_layer
saved_collision_mask = collision_mask
roll()
if not is_rolling:
var move_dir = movement_vector().normalized()
velocity = move_dir
move_and_slide()
if deg_timer.is_stopped():
if current_weapon:
if current_weapon.rotation_degrees > dves or current_weapon.rotation_degrees < dev:
current_weapon.position = Vector2(-2, -5)#right
dves -= switch_buffer
dev += switch_buffer
else:
current_weapon.position = Vector2(2, -5)#left
dves += switch_buffer
dev -= switch_buffer
deg_timer.start()
func _physics_process(delta):
if is_rolling:
velocity = Vector2.ZERO
var motion = roll_direction * (roll_distance/roll_duration) * delta
move_and_collide(motion)
else:
var move_dir = movement_vector().normalized()
velocity = move_dir * speed
move_and_slide()
func roll():
if is_rolling:
return
is_rolling = true
if current_weapon:
current_weapon.hide()
roll_direction = movement_vector().normalized()
if roll_direction == Vector2.ZERO:
pass
current_animation = "roll"
animated_sprite_2d.play(current_animation + str(a))
var start_position = position
var target_position = start_position + roll_direction * roll_distance
collision_mask = 1
collision_layer = 0
#velocity_backup = velocity
#velocity = roll_direction * (roll_distance / roll_duration)
var tween = get_tree().create_tween()
tween.tween_property(self, "position", target_position, roll_duration).set_trans(Tween.TRANS_QUAD).set_ease(Tween.EASE_OUT)
tween.tween_callback(end_roll)
roll_timer.start()
func end_roll():
is_rolling = false
animated_sprite_2d.play("idle" + str(a))
if current_weapon:
current_weapon.show()
collision_layer = saved_collision_layer
collision_mask = saved_collision_mask
func _on_deg_timer_timeout():
dves = 270
dev = 90
func pick_up_weapon(weapon_pickup):
var new_weapon = weapon_pickup.weapon.instantiate()
var weapon_data = {
"scene": weapon_pickup.weapon,
"current_ammo": new_weapon.current_ammo,
"reserve_ammo": new_weapon.reserve_ammo,
}
inventory.append(weapon_data)
if inventory.size() == 1:
equip_weapon(0)
weapon_pickup.queue_free()
nearby_weapon = null
func equip_weapon(index: int):
if inventory.is_empty():
return
if current_weapon:
inventory[current_weapon_index] ["current_ammo"] = current_weapon.current_ammo
inventory[current_weapon_index] ["reserve_ammo"] = current_weapon.reserve_ammo
current_weapon.queue_free()
current_weapon = null
var weapon_data = inventory[index]
var new_weapon = weapon_data["scene"].instantiate()
weapons.add_child(new_weapon)
new_weapon.position = Vector2(10, -5)
current_weapon = new_weapon
current_weapon_index = index
current_weapon.current_ammo = weapon_data["current_ammo"]
current_weapon.reserve_ammo = weapon_data["reserve_ammo"]
current_weapon.update_ammo_ui()
func quick_switch_weapon():
if inventory.size() < 2:
return
var previous_index = (current_weapon_index + 1) % inventory.size()
equip_weapon(previous_index)
switch_cooldown.start()
func next_switch_weapon():
if inventory.size() < 2:
return
var next_index = (current_weapon_index - 1) % inventory.size()
equip_weapon(next_index)
switch_cooldown.start()
func previous_switch_weapon():
if inventory.size() < 2:
return
var previous_index = (current_weapon_index + 1) % inventory.size()
equip_weapon(previous_index)
switch_cooldown.start()
func movement_vector():
var movement_x = Input.get_action_strength("right") - Input.get_action_strength("left")
var movement_y = Input.get_action_strength("down") - Input.get_action_strength("up")
return Vector2(movement_x, movement_y)
func _on_area_2d_area_entered(area):
health_component.take_damage(1)
func _on_roll_timer_timeout():
end_roll()
#is_rolling = false
#set_deferred("collision_layer", saved_collision_layer)
#set_deferred("collision_mask", saved_collision_mask)