- Home /
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 $$anonymous$$e but they are asking for different things that I cant change to fit what Im trying to do.
Answer by Hi There · Mar 25, 2013 at 05:10 PM
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.
there are several ways to find out which object are you colliding with. Check this: http://forum.unity3d.com/threads/55299-Unity-Collision-tutorials
You could use the OnTriggerEnter method. For instance, if you use the player to manage it, would be something like this (c#):
void OnTriggerEnter (Collider other ) { //Get the Component of the weapon.
Weapon weaponFound = other.GetComponent<Weapon>();
if ( weaponFound ) {
//Debug.Log("weapon Found");
//change parent and position with current weapon
} else {
//Debug.Log("Wasn't a weapon");
// Doesn't matter
}
}
I didn't try it, sorry! but you get the idea, right? Hope it helps!
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 Swap$$anonymous$$yWeaponWithThisNewAwesomeOne ( 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 $$anonymous$$14 leaving everything else deactivated. Can you help me out?
I posted the code in the question.
It depends in your actual implementation. If you have a variable, storing the player's current weapon, in the player script, you could just do something like calling a method, sending the new uzi as a parameter: SwapWeapons( collision.gameObject );
where: function SwapWeapons( newWeapon : gameobject ) { // "$$anonymous$$iddle man" object var fooWeapon : gameobject;
//store any parameters needed in a "middle man" object
fooWeapon.transform.position = newWeapon.transform.position;
// adopt the new weapon
newWeapon.transform.position = currentWeapon.transform.positoin;
newWeapon.parent = currentWeapon.parent;
// drop previous weapon
currentWeapon.transform.position = fooWeapon.transform.position;
// current weapon is no longer the current one...
currentWeapon.parent = null;
currentWeapon = newWeapon;
}
// sorry, I don't use java! so, again, i didn't try it! hopes it helps anyway!
Your answer
Follow this Question
Related Questions
Ignore collision on tag not working 2 Answers
Create tag in script, then check for it in collision 1 Answer
OnCollision Script... 2 Answers
Activating and deactivating problem 2 Answers
C# How to have weapon pickup 1 Answer