Gun swap through collision

Ive been trying to add this feature to my game for a very long time now with no luck what so ever.

What I am wanting to do is start my game with the player holding a gun, the gun being a child of the main camera and being tagged as gun1. When the player collides with an object on the ground tagged as gun2 they swap around, so gun1 is now the object on the ground and gun2 is the child of the main camera. There will also be gun3 and gun4, but the principle is which ever gun you collide with it swaps with the gun attached to the main camera. Can someone help me with this please?

var glock : GameObject;
var UZI : GameObject;
var G36C : GameObject;
var M14 : GameObject;

function Start () {

UZI.active = false;
glock.active = false;
G36C.active = false;
M14.active = false;
}

function OnCollisionEnter(collision: Collision) {
 
if (collision.gameObject.tag == "UZI");

UZI.active = true;
glock.active = false;
G36C.active = false;
M14.active = false;



 
if (collision.gameObject.tag == "glock");

UZI.active = false;
glock.active = true;
G36C.active = false;
M14.active = false;



 
if (collision.gameObject.tag == "G36C");

UZI.active = false;
glock.active = false;
G36C.active = true;
M14.active = false;



 
if (collision.gameObject.tag == "M14");

UZI.active = false;
glock.active = false;
G36C.active = false;
M14.active = true;
}

Yeah Ive looked at a lot of other questions similar to mine but they are asking for different things that I cant change to fit what Im trying to do.

1 Answer

1

where exactly are you finding problems? are you able to detect when the player collides with gun2?
The player could have a variable storing the current weapon, for instance, so when you collide with another one, you just have to swap their parent objects

I cant figure out how the Player knows when it has collided with something tagged as gun1, 2, 3 or 4. And how to swap the guns around when they do collide.

ups. maybe you should get the gameobject first, in order to access its components (the weapon script for example). other.gameObject.GetComponent<Weapon>(); and maybe you prefer to get the tag, to check if the object was a weapon: if(other.gameObject.tag == "Weapon2"){ // do something with that information! // for instance, call a method to swap the weapons SwapMyWeaponWithThisNewAwesomeOne ( other.gameObject ); } anyway, hope you get the idea!

Im trying to use javascript but from what you have given me I have wrote this code that activates and deactivates my guns depending on what tag the player collides with. The only problem is which ever tagged object I collide with it only enables the M14 leaving everything else deactivated. Can you help me out? I posted the code in the question.

you have to use curly brackets in the if conditions, or it will always execute other but the first sentence... For instance: if (collision.gameObject.tag == "UZI") { UZI.active = true; glock.active = false; G36C.active = false; M14.active = false; }

I think ill open a new question explaining my problem better, but thank you for your help I am a lot closer to my goal than before.