Hello, I'm just starting with Godot and I'm having trouble figuring this out. I have a Bullet scene which is on layer 5, mask 1, 2 and 4, and a MovableBox scene, which is on layer 6, mask 1. Both extend RigidBody2D. Despite not sharing any layers or masks, when a bullet hits the MovableBox, it moves backwards. This is the code for the Bullet scene:
class_name Bullet
extends RigidBody2D
var _start_pos = 0
var exploded = false
const MAX_DISTANCE = 1000
var sound_explode = load("res://assets/audio/sfx/explode.wav")
func init(position):
_start_pos = position
set_gravity_scale(0)
func _physics_process(delta):
if position.x - _start_pos.x > MAX_DISTANCE:
# Use SFX_manager because queue_free() removes the sound effect.
SFX_manager.play_sound(sound_explode)
queue_free()
func destroy():
# animation_player.play("destroy")
print("destroy")
func _on_body_entered(body):
print("destroyed")
# if body is Enemy:
# body.destroy()
And the MovableBox has no code yet. It may be worth mentioning that the bullet is instantiated from the Gun scene, which is called from the Player scene. I was thinking it may have to do with this. The Gun doesn't have an Area node for layers/masks, but the Player scene is on layer 1, mask 4.
class_name Gun
extends Position2D
const BULLET_VELOCITY = 2500.0
const Bullet = preload("res://src/Objects/Bullet.tscn")
onready var sound_shoot = $Shoot
# This method is only called by Player.gd.
func shoot(direction = 1):
var bullet = Bullet.instance()
bullet.init(global_position)
bullet.global_position = global_position
bullet.linear_velocity = Vector2(direction * BULLET_VELOCITY, 0)
bullet.set_as_toplevel(true)
add_child(bullet)
sound_shoot.play()
return true
extends Actor
const ACC = 50
const MIN_JUMP_HEIGHT = -1000
onready var sprite = $AnimatedSprite
onready var gun = $AnimatedSprite/Gun
var _hold_fire_timer
func _ready():
_hold_fire_timer = Timer.new()
add_child(_hold_fire_timer)
_hold_fire_timer.set_one_shot(true)
_hold_fire_timer.set_wait_time(0.5)
func _physics_process(delta):
var direction = get_direction()
var is_jump_interrupted := Input.is_action_just_released("jump") and _velocity.y < 0.0
if is_jump_interrupted:
interrupt_jump()
direction = get_direction()
if direction.y != 0.0:
jump(direction)
# If the character is moving or gets new input to walk.
if _velocity.x != 0.0 or direction.x != 0.0:
walk(direction)
_velocity = move_and_slide(_velocity, FLOOR_NORMAL)
# When the character’s direction changes, scale the sprite to flip it.
if direction.x != 0:
sprite.scale.x = 1 if direction.x > 0 else -1
# Use the sprite's scale to store the look direction, enables shooting forward.
if Input.is_action_pressed("shoot") and _hold_fire_timer.is_stopped():
gun.shoot(sprite.scale.x)
_hold_fire_timer.start()
if Input.is_action_just_released("shoot"):
_hold_fire_timer.stop()
var animation = get_new_animation(_velocity)
if animation != sprite.animation:
sprite.animation = animation
sprite.play()
func jump(direction):
_velocity.y = speed.y * direction.y
func interrupt_jump():
if _velocity.y < MIN_JUMP_HEIGHT:
_velocity.y = MIN_JUMP_HEIGHT
func walk(direction):
var velocity = _velocity
var desired_x = speed.x * direction.x - velocity.x
velocity += Vector2(clamp(desired_x, -ACC, ACC), 0)
_velocity.x = velocity.x
func get_direction():
return Vector2(
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
(
-Input.get_action_strength("jump")
if is_on_floor() and Input.is_action_pressed("jump")
else 0.0
)
)
func get_new_animation(velocity):
var animation_new = "idle"
if velocity.x != 0:
animation_new = "walk"
if velocity.y != 0:
animation_new = "jump"
return animation_new
```
If you need more information, please let me know. Thank you!