0 votes

Hello Everyone
I'm currently working on a Weapon Pickup/Drop System. I had a few questions on how should I go on about doing this. I have these different types of weapons(pistol, shotgun, rifle, etc) that I've placed in the scene and want to pick them up by pressing the interact key. So far I have a basic gun in the game that I can interact with. How should I go about having different types of weapons and how would I reference them in my character for the logic. So far this is the script for the character and weapon interaction

extends Camera

# Global Variables
onready var InteractCast = $InteractCast
onready var WeaponCast = $WeaponCast
onready var Muzzle = $Hand/Weapon/GunModel/Muzzle
onready var Hand = $Hand

# Local Variables
var WeaponEquipped = false
var Weapon

func _ready():
    pass

func _physics_process(delta):
    if Input.is_action_just_pressed("Interact"):
        if InteractCast.is_colliding():
            var target = InteractCast.get_collider()
            if target.is_in_group("Weapons"):
                Weapon = target
                print("InteractRaycast Hit Weapon")
                target.get_parent().remove_child(Weapon)
                target.add_child(Weapon)
                WeaponEquipped = true
            else:
                print("InteractRaycast Collided With Something")
        else:
            print("InteractRaycast Did Not Collide With Anything")
    if Input.is_action_just_pressed("Drop"):
        if WeaponEquipped:
            Hand.remove_child("Weapon")
            Weapon.set_parent(get_node("/root"))
            WeaponEquipped = false
            print(WeaponEquipped)
        else:
            print("Cant drop Weapon not Equipped!")

    if Input.is_action_just_pressed("FireWeapon"):
        if WeaponEquipped:
            if WeaponCast.is_colliding():
                var bullet = get_world().direct_space_state
                var collision = bullet.intersect_ray(Muzzle.transform.origin, WeaponCast.get_collision_point())
                if collision:
                    var target = collision.collider
                    print(target)
                    if target.is_in_group("Enemies"):
                        print("Weapon Hit Enemy")
                else:
                    print("Weapon Hit Nothing")
in Engine by (14 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.