Godot 4
What the projectile is the post to do is when you click in the mouse it shoots from the player and if it collides with a certain object , it will reverse it’s gravity of the object but
Basically the projectile does not work and I will show you what it does
Basically it does not spawn on the player and is affected by gravity i used chat g b t to help me of most of the code
( Player’s code)
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
const GRAVITY = 1200.0
@onready var Animated_sprite = $AnimatedSprite2D
@export var projectile_scene: PackedScene # Assign the projectile scene in the inspector
@export var shoot_offset = Vector2(10, 0) # Offset from the player to spawn the projectile
@export var shoot_speed = 500.0 # Speed of the projectile
@export var shoot_cooldown = 0.5 # Time between shots
var time_since_last_shot = 0.0 # Cooldown timer
func _physics_process(delta: float) → void:
time_since_last_shot += delta
# Shooting input handling
if Input.is_action_just_pressed("shoot") and time_since_last_shot >= shoot_cooldown:
print("Shoot button pressed") # Debug: Confirm shoot button press
shoot_projectile()
time_since_last_shot = 0.0 # Reset cooldown timer
# Apply gravity if not on the floor.
if not is_on_floor():
velocity.y += GRAVITY * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction (left/right).
var direction := Input.get_axis("move_left", "move_right")
# Control animations and sprite flipping.
if direction > 0:
Animated_sprite.flip_h = false
elif direction < 0:
Animated_sprite.flip_h = true
if is_on_floor():
if direction == 0:
Animated_sprite.play("idle")
else:
Animated_sprite.play("Run")
else:
Animated_sprite.play("jump")
# Update horizontal movement based on input.
if direction != 0:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
# Apply movement.
move_and_slide()
func shoot_projectile() → void:
print(“Attempting to shoot…”) # Debug message to confirm the function is called
# Check if projectile_scene is assigned
if projectile_scene == null:
print("Error: projectile_scene is not assigned!")
return
# Create an instance of the projectile
var projectile = projectile_scene.instantiate()
if projectile != null:
print("Projectile instantiated successfully!") # Debug message
else:
print("Error: Could not instantiate projectile!")
return
# Print player's rotation for debugging
print("Player Rotation: ", rotation)
# Use the player's global position and apply an offset based on the player's rotation
var spawn_offset = Vector2(50, 0).rotated(rotation) # Adjust the offset to control how far in front of the player it spawns
projectile.position = global_position + spawn_offset # Ensure it spawns relative to the player
# Debugging: Print the player's and projectile's positions
print("Player Position: ", global_position)
print("Projectile Spawn Position: ", projectile.position)
# Set the projectile's rotation to match the player's rotation
projectile.rotation = rotation
# Add the projectile to the current scene
get_tree().current_scene.add_child(projectile)
print("Projectile added to the scene!") # Debug message
# Apply impulse to move the projectile forward
if projectile is RigidBody2D:
var impulse_direction = Vector2.RIGHT.rotated(rotation) * shoot_speed
print("Applying Impulse: ", impulse_direction)
projectile.apply_impulse(impulse_direction)
print("Impulse applied to the projectile!") # Debug message
else:
print("Projectile is not a RigidBody2D!")
( Code for projectile)
extends RigidBody2D
@export var speed = 400.0 # Exported variable for speed
func _ready() → void:
contact_monitor = true # Enable contact monitoring
max_contacts_reported = 1 # Set the maximum number of contacts reported
apply_impulse(Vector2.RIGHT.rotated(rotation) * speed) # Apply an impulse for movement
# Correct the signal connection using a Callable object
connect("body_entered", Callable(self, "_on_body_entered"))
Handle collisions when the projectile enters the collision area of another object
func _on_body_entered(body: Node) → void:
print("Collided with: ", body.name) # Debug print
if body is CharacterBody2D:
print(“CharacterBody2D detected”)
# Reverse the gravity effect by inverting the velocity.y
if body.has_method(“set_velocity”):
body.velocity.y = -body.velocity.y # Inverts the vertical velocity
if body.has("gravity"):
print("Reversing gravity")
body.gravity = -body.gravity # Reverse gravity if the body has a gravity variable
queue_free() # Destroy the projectile after impact
And if you need the code for the object in question let me know
Also thank you