Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Blink · Mar 25, 2013 at 03:17 PM · collisiontaggunswap

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;
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blink · Mar 25, 2013 at 04:16 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blink · Mar 25, 2013 at 05:33 PM 0
Share

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.

avatar image Hi There · Mar 25, 2013 at 06:02 PM 0
Share

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!

avatar image Hi There · Mar 25, 2013 at 06:15 PM 0
Share

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!

avatar image Blink · Mar 26, 2013 at 11:52 AM 0
Share

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.

avatar image Hi There · Mar 26, 2013 at 12:58 PM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges