VERSION: 4.3
Im trying to make like the main weapon of the character spawn from him and go in a straight line, it is going in a straight line but it keeps spawning on top of the character, Why? Im not sure…
but is definitely something i did.
Code inside projectile/Glove:
extends Area2D
var Speed = -450
func _process(delta):
position.x -= Speed * delta
pass
bullet code inside CharBody2D:
func shoot():
var glove = Bulleto.instantiate()
var horizontal_offset = -20
var vertical_offset = -10
bullet.position = sprite.position
if sprite.flip_h:
bullet.position = position + Vector2(-horizontal_offset, vertical_offset)
else:
bullet.position = position + Vector2(horizontal_offset, vertical_offset)
get_parent().add_child(glove)
Hole code:
const SPEED = 500
const MOONWALK_SPEED = 750
const JUMP_VELOCITY = -560
const JUMP_MAX_HEIGHT = -760
const Heli_S = 150
var is_gliding = false
@onready var sprite = $AnimatedSprite2D
@onready var rc = $RayCast2D
@onready var GRC = $GliderRC
@onready var Bulleto = preload("res://glove.tscn")
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var is_jumping = false
func _physics_process(delta):
if not is_on_floor():
if is_gliding:
if velocity.y < Heli_S:
velocity.y = Heli_S
else:
velocity.y += gravity * delta
else:
is_gliding = false
if Input.get_action_strength("ui_accept") && is_on_floor():
velocity.y = JUMP_VELOCITY
is_jumping = true
if Input.get_action_strength("high_jump") && is_on_floor():
velocity.y = JUMP_MAX_HEIGHT
is_jumping = true
if Input.is_action_pressed("Helicopter_1") and not is_on_floor():
is_gliding = true
else:
is_gliding = false
if not is_on_floor() and is_gliding:
if sprite.animation != "Helicopter":
sprite.play("Helicopter")
elif not is_on_floor() and not is_gliding:
if sprite.animation != "jump":
sprite.play("jump")
var direction = Input.get_axis("ui_left", "ui_right")
var is_running_right = Input.is_action_pressed("Running_Right")
var is_running_left = Input.is_action_pressed("Running_Left")
if is_running_right and is_on_floor():
velocity.x = Runnin_VV
sprite.play("Running")
sprite.flip_h = false
elif is_running_left and is_on_floor():
velocity.x = -Runnin_VV
sprite.play("Running")
sprite.flip_h = true
elif direction != 0:
velocity.x = direction * SPEED
if sprite.animation != "Walk" and is_on_floor():
sprite.play("Walk")
else:
velocity.x = 0
if is_on_floor() and sprite.animation != "idle":
sprite.play("idle")
if Input.is_action_pressed("moon_walk") and is_on_floor() and not is_running_left and not is_running_right:
if direction != 0 and rc.is_colliding():
sprite.play("Walk")
velocity.x = -MOONWALK_SPEED * direction
else:
velocity.x = -MOONWALK_SPEED * sign(velocity.x)
if Input.is_action_just_pressed("ui_left"):
sprite.flip_h = true
elif Input.is_action_just_pressed("ui_right"):
sprite.flip_h = false
move_and_slide()
if Input.is_action_just_pressed("glove shot"):
shoot()
func shoot():
var bullet = Bulleto.instantiate()
var horizontal_offset = -20
var vertical_offset = -10
bullet.position = sprite.position
if sprite.flip_h:
bullet.position = position + Vector2(-horizontal_offset, vertical_offset)
else:
bullet.position = position + Vector2(horizontal_offset, vertical_offset)
get_parent().add_child(bullet)
Any Help is appreciated!