x


Weapon swapping in-game

Hi there I am planning on having around 15 different guns within my game and am wondering how to change weapons from the one in my hand to a weapon on the ground. I have a firing/ammo script for my guns and I have a gun swapping script that I made:

var MyHand : Transform; //your hand
var MyGun : Transform; //what your picking up
var ThatGun : Transform;
var dist = 5; //distance at which you can pick things up
private var isHolding = false;

    function Update () {
        if(Input.GetKeyDown(KeyCode.Q)){
            if(Vector3.Distance(transform.position, MyGun.position) < dist)
            {
                isHolding = !isHolding;
                }
                }

        if(isHolding == true){
            MyHand.transform.DetachChildren();
            MyGun.rigidbody.useGravity = false;
            MyGun.parent = MyHand;
            MyGun.transform.position = MyHand.transform.position;
            MyGun.transform.rotation = MyHand.transform.rotation;
            }
            else{
                MyHand.transform.DetachChildren();
                MyGun.rigidbody.useGravity = true;
                ThatGun.parent = MyHand;
                ThatGun.transform.position = MyHand.transform.position;
                ThatGun.transform.rotation = MyHand.transform.rotation;
            }
    }

the problem is that this script that I made-up only works for two guns and I need to swap between around 15 eventually. I know I could extend this script to work for more guns but I'm worried it would get very complicated and slow functioning.

Any help would be much appreciated

Thanks, Scribe (javascript please)

more ▼

asked Feb 28 '11 at 07:54 PM

Scribe gravatar image

Scribe
1.4k 12 16 34

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You are testing against a particular gun that you have to assign to your player. Just tag your guns as "Gun" and do something like that:

var MyHand : Transform;
var CurrentGun : Transform;
var dist : float = 5;

function Update() {
    if (Input.GetKeyDown(KeyCode.Q)){
        var allNearObjects : Collider[] = Physics.OverlapSphere(transform.position,dist);
        for (var current : Collider in allNearObjects) {
            if (current.transform != CurrentGun && current.tag == "Gun") {
                // Detach old gun
                CurrentGun.parent = null; 
                CurrentGun.rigidbody.isKinematic = false;
                //Attach new gun
                CurrentGun = current.transform;
                CurrentGun.rigidbody.isKinematic = true;
                CurrentGun.parent = MyHand;
                CurrentGun.localPosition = Vector3.zero;
                CurrentGun.localRotation = Quaternion.identity;
                break;
            }
        }
    }
}

When hitting the "Q" button it iterated through all colliders that are in range (dist). If it found a gameObject that is tagged as "Gun" and it's not the one you're using at the moment. switch them.

more ▼

answered Feb 28 '11 at 09:00 PM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

thanks worked great

Mar 01 '11 at 04:50 PM Scribe
(comments are locked)
10|3000 characters needed characters left

Make an array of guns, and just have it change due to amount, and have a switch statement for certain values...

A = 0, B=1, C=2, D=3.

Switch so that if A, gun value = 0, and so you change var gun to that gun, and all the properties... B has value = 1 etc.

Kind of easy.

more ▼

answered Feb 28 '11 at 08:13 PM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3460
x379
x220
x10

asked: Feb 28 '11 at 07:54 PM

Seen: 1210 times

Last Updated: Feb 28 '11 at 07:54 PM