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 kinkaid1911 · Aug 30, 2012 at 08:58 PM · fpsweaponpickup

picking up weapons script?

i'm making a small game where you choose a rifle from a rack and take it to a range. The issue is, every pickup script I see is made to use 2 or more switchable weapons. With some help, I started a script, but I suck at scripting in the first place. I parented a small sphere to the main camera that would swap out with a weapon on one of the racks when you get close enough, but I can't seem to get it to work. I do have my controller tagged as "player". Here's my script:

 var SpawnTo:Transform;
 var Gun : Transform;
 function OnCollisionEnter(hit:Collision)
 {
 if(hit.gameObject.tag == "Player")
 {
 Gun.parent = SpawnTo;
 Gun.transform.position = SpawnTo.transform.position;
 Gun.transform.rotation = SpawnTo.transform.rotation;
 }
 }
 

 
 

I could use help ASAP

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by djscriptify · Aug 31, 2012 at 12:28 AM

I don't get why that doesn't work, but I usually use triggers (just a personal preference). Here's how I would do it: (JavaScript)

 //Remember to check is trigger on the gun's collider 
 
 var spawnTo : Transform; 
 var gun : gameObject;
 
 function OnTriggerEnter(col : Collider){
 
 if(col.tag == "Player"){
 
 print("Player touched weapon!");
 
 gun.parent = spawnTo; 
 gun.transform.position = spawnTo.transform.position; 
 gun.transform.rotation = spawnTo.transform.rotation;
 
 }
 
 }
Comment
Add comment · Show 1 · 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 SchwifD · Mar 26, 2015 at 04:26 AM 0
Share

Thanks this worked great, and a simple way to use this for multiple items is to turn gun variable in an array!

avatar image
0

Answer by kinkaid1911 · Sep 04, 2012 at 12:37 AM

apparently, the script itself worked! thank you!

issue is, cosmetically it didn't. the weapon didn't spawn on the sphere and I ended up getting this error:

MissingFieldException: Field 'UnityEngine.GameObject.parent' not found.

Comment
Add comment · 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
0

Answer by chrisall76 · Sep 04, 2012 at 03:09 AM

This code should get you on the right track:

 var SelectedWeapon : GameObject; //Current equipped weapon
 
 var Primary : GameObject;     //Primary weapon
 
 var isLocked = false; //Checks if switching is off
 
 function Start () {
     SelectedWeapon = Primary; //Selects Primary weapon
 }
 
 function Update () {
 
     if(Input.GetKey(KeyCode.LeftShift)){
         if(isLocked){
             isLocked = true;
         }
         
         if(!isLocked){
             isLocked = false;
         }
     }
 
     if(Input.GetKey(KeyCode.Alpha1) && (!isLocked)){
         SelectedWeapon = Primary;
     }
     
     if(SelectedWeapon == Primary){
         SelectedWeapon = Primary;
     }
 }

 function ChangeWeapon(WeaponType : String){
    WeaponType = "Primary";
 }
 
 function ChangeWeapon(WeaponObject : GameObject, WeaponType : String){
 
     if(WeaponType == "Primary"){
         Primary = WeaponObject;
     }
 }

Only thing is that for changing, make sure the weapon is in the hand just like the others but just inactivated. When you want to change what weapon you use, just call the ChangeWeapon function. Designed this script just for this cause too :)

Comment
Add comment · Show 2 · 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 kinkaid1911 · Sep 04, 2012 at 05:57 PM 0
Share

Issue with your code is i'm not making it so you have 2 or 3 weapons. you can only carry one. Thank you though

avatar image chrisall76 · Sep 05, 2012 at 01:37 AM 0
Share

Fixed for one weapon.

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

10 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

Related Questions

picking up guns 1 Answer

Weapon pick up 3 Answers

Weapon pick up and switching script 2 Answers

How to make a weapon index? 1 Answer

FPS Weapon and animation 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