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 HamCoder404 · Jan 26, 2014 at 01:58 AM · guiraycastweaponweapons

GUI display weapon name you are looking at

Hello. I don't always ask questions on Unity Answers, but when I do, I need help REAL bad.

I have a First Person Shooter game made with FPS Kit 2 from [ArmedUnity.][1] When the player looks at a gun (using the Raycast Physics function) I want the GUI on the main camera to show the weapon name along with the price (I have not started that yet). Here is what I have so far:

 // WeaponIndex.JS //
 
 public var weaponPrice : int;
 public var setWeapon : int; // Weapon Index
 public var weaponName : String;
 
 function Update () {
     weaponName = gameObject.name;
 }

This script (WeaponIndex.JS) is attached to every pickup weapon, and each weapon has a certain index (setWeapon). Then I have a weapon manager script attached to the main camera with the GUI stuff. Here is the onGUI function:

 /**
 *  Script written by OMA [www.armedunity.com]
 **/
 
 
 var weaponsInUse : GameObject[];                    // used weapons, among which you can switch.
 var weaponsInGame : GameObject[];                    // all weapons, which could be used in game 
 var worldModels : Rigidbody[];                         // just a prefab which could be instantiated when you drop weapon
 
     var hit : RaycastHit;
     var distance : float = 2.0;
     var layerMaskWeapon : LayerMask;
     var layerMaskAmmo : LayerMask;
     
     var dropPosition : Transform;
     var weapIndex : WeaponIndex;
     
     var switchWeaponTime : float = 0.5;
     @HideInInspector
     var canSwitch : boolean = true;
     @HideInInspector
     var showWepGui : boolean = false;
     @HideInInspector
     var showAmmoGui : boolean = false;
     private var equipped : boolean = false;
     //@HideInInspector
     //var i : int = 0;
     //@HideInInspector
     var weaponToSelect : int;
     //@HideInInspector
     var setElement : int;
     //@HideInInspector
     var weaponToDrop : int;
     var mySkin : GUISkin;
     var pickupSound : AudioClip;
     private var textFromPickupScript : String = "";
     private var notes : String = "";
     private var note : String = "Press key <E> to pick up Ammo";
     private var note2 : String = "Select appropriate weapon to pick up ammo";
     
     var weaponName : String;
     var weaponPrice : int;
 
 function Start (){
     for (var h : int = 0; h < worldModels.length; h++){
         weaponsInGame[h].gameObject.SetActive(false);
     }    
     weaponToSelect = 0;
     DeselectWeapon();
 }
 
 
 function Update () {
     //weaponName = weapIndex.weaponName;
     //weaponPrice = weapIndex.weaponPrice;
     
     if (Input.GetKeyDown("1") && weaponsInUse.length >= 1 && canSwitch && weaponToSelect != 0) {
         DeselectWeapon();
         weaponToSelect = 0;
 
     } else if (Input.GetKeyDown("2") && weaponsInUse.length >= 2 && canSwitch && weaponToSelect != 1) {
         DeselectWeapon();
         weaponToSelect = 1;
 
     }
     
     if (Input.GetAxis("Mouse ScrollWheel") > 0 && canSwitch){
             weaponToSelect++;
         if (weaponToSelect > (weaponsInUse.length - 1)){
             weaponToSelect = 0;
         }
         DeselectWeapon();
     }
     
     if (Input.GetAxis("Mouse ScrollWheel") < 0 && canSwitch){
         weaponToSelect--;
         if (weaponToSelect < 0){
             weaponToSelect = weaponsInUse.length - 1;
         }
         DeselectWeapon();
     }
     
     var position = transform.parent.position;
     var direction : Vector3 = transform.TransformDirection (Vector3.forward);
     if (Physics.Raycast (position, direction, hit, distance, layerMaskWeapon.value)){
     //Debug.Log(hit.collider.gameObject.weapIndex.weaponPrice);
     weaponName = hit.collider.gameObject.name;
     var prefab : WeaponIndex = hit.transform.GetComponent("WeaponIndex");
     //weaponPrice = GameObject.Find(hit.collider.gameObject.name).GetComponent("WeaponIndex").weaponPrice;
     //Debug.Log(RaycastHit.hit.transform.gameObject.GetComponent(WeaponIndex).weaponPrice);
         setElement = prefab.setWeapon;
         weaponPrice = prefab.weaponPrice;
         showWepGui = true;
                                                                                                             //if you want more than 2 weapons equip at the same time        
         if(weaponsInUse[0] != weaponsInGame[setElement] && weaponsInUse[1] != weaponsInGame[setElement]){ //&& weaponsInUse[2] != weaponsInGame[setElement] && weaponsInUse[3] != weaponsInGame[setElement]){
             equipped = false;
         }else{
             equipped = true;
         }
         
         if(canSwitch){
             if(!equipped && Input.GetKeyDown ("e")){
                 DropWeapon(weaponToDrop);
                 DeselectWeapon();
                 weaponsInUse[weaponToSelect] = weaponsInGame[setElement];
                     if(setElement == 8){
                         var pickupGOW1 : Pickup = hit.transform.GetComponent("Pickup");
                         addStickGrenades(pickupGOW1.amount);
                     }    
                 Destroy(hit.collider.transform.parent.gameObject);
                 
             }else{    
             
                 if(setElement == 8){
                     if(Input.GetKeyDown ("e")){
                         var pickupGOW : Pickup = hit.transform.GetComponent("Pickup");
                         addStickGrenades(pickupGOW.amount);
                         Destroy(hit.collider.transform.parent.gameObject);
                     }
                 }
             }            
         }
     
     }else{
         showWepGui = false;
     }
     
     if (Physics.Raycast (position, direction, hit, distance, layerMaskAmmo.value)){
         weaponName = hit.collider.gameObject.name;
         //weaponPrice = hit.collider.gameObject.transform.GetComponent(WeaponIndex).weaponPrice;
         Debug.Log(RaycastHit.hit.transform.gameObject.GetComponent(WeaponIndex).weaponPrice);
         showAmmoGui = true;
         if(hit.transform.CompareTag("Ammo")){
             var pickupGO : Pickup = hit.transform.GetComponent("Pickup");
             
             //ammo for pistols, rifles 
             if (pickupGO.pickupType == PickupType.Magazines) {
                 var mags : WeaponScriptNEW = weaponsInUse[weaponToSelect].gameObject.transform.GetComponent("WeaponScriptNEW");
                 if(mags != null && mags.firstMode != fireMode.launcher){
                     notes = "";
                     textFromPickupScript = note;
                     if(Input.GetKeyDown ("e")){
                         if(mags.ammoMode == Ammo.Magazines){
                             mags.magazines += pickupGO.amount;
                         }else{
                             mags.magazines += pickupGO.amount * mags.bulletsPerMag;
                         }    
                         audio.clip = pickupSound;
                         audio.Play();    
                         Destroy(hit.collider.gameObject);
                     }    
                 }else{
                     textFromPickupScript = pickupGO.AmmoInfo;
                     notes = note2;
                 }
             }
             //ammo for Sniper rifle
             if (pickupGO.pickupType == PickupType.SniperMagazines) {
                 var magsSniper : SniperScript = weaponsInUse[weaponToSelect].gameObject.transform.GetComponent("SniperScript");
                 if(magsSniper != null){
                     notes = "";
                     textFromPickupScript = note;
                     if(Input.GetKeyDown ("e")){
                         magsSniper.magazines += pickupGO.amount;
                         audio.clip = pickupSound;
                         audio.Play();    
                         Destroy(hit.collider.gameObject);
                     }    
                 }else{
                     textFromPickupScript = pickupGO.AmmoInfo;
                     notes = note2;
                 }    
             }
             //ammo for weapon if second fireMode is luancher
             if (pickupGO.pickupType == PickupType.Projectiles) {
                 var projectile : WeaponScriptNEW = weaponsInUse[weaponToSelect].gameObject.transform.GetComponent("WeaponScriptNEW");
                 if(projectile != null && projectile.secondMode == fireMode.launcher){
                     notes = "";
                     textFromPickupScript = note;
                     if(Input.GetKeyDown ("e")){
                         projectile.projectiles += pickupGO.amount;
                         audio.clip = pickupSound;
                         audio.Play();    
                         Destroy(hit.collider.gameObject);
                     }    
                 }else{
                     textFromPickupScript = pickupGO.AmmoInfo;
                     notes = note2;
                 }
             }
             //ammo for rocket launcher
             if (pickupGO.pickupType == PickupType.Rockets) {
                 var rockets : WeaponScriptNEW = weaponsInUse[weaponToSelect].gameObject.transform.GetComponent("WeaponScriptNEW");
                 if(rockets != null && rockets.firstMode == fireMode.launcher){
                     notes = "";
                     textFromPickupScript = note;
                     if(Input.GetKeyDown ("e")){
                         rockets.projectiles += pickupGO.amount;
                         rockets.EnableProjectileRenderer();
                         audio.clip = pickupSound;
                         audio.Play();    
                         Destroy(hit.collider.gameObject);    
                     }    
                 }else{
                     textFromPickupScript = pickupGO.AmmoInfo;
                     notes = note2;
                 }    
             }
             //ammo for shotgun
             if (pickupGO.pickupType == PickupType.Shells) {
                 var bullets : ShotGunScriptNEW = weaponsInUse[weaponToSelect].gameObject.transform.GetComponent("ShotGunScriptNEW");
                 if(bullets != null){
                     notes = "";
                     textFromPickupScript = note;
                     
                     if(Input.GetKeyDown ("e")){
                         bullets.magazines += pickupGO.amount;
                         audio.clip = pickupSound;
                         audio.Play();    
                         Destroy(hit.collider.gameObject);
                     }
                 }else{
                     textFromPickupScript = pickupGO.AmmoInfo;
                     notes = note2;
                 }
             }
             //pickup health
             if (pickupGO.pickupType == PickupType.Health) {
                 textFromPickupScript = pickupGO.AmmoInfo;
                 notes = "";
                 if(Input.GetKeyDown ("e")){
                     var playerGO = GameObject.Find("Player"); 
                     var hp : PlayerDamageNew = playerGO.gameObject.transform.GetComponent("PlayerDamageNew");
                     hp.hitPoints += pickupGO.amount;
                     audio.clip = pickupSound;
                     audio.Play();     
                     Destroy(hit.collider.gameObject);
                 }
             }
         }    
     
     }else{
         showAmmoGui = false;
     }    
 }
 
 function addStickGrenades(amount : int){
     yield WaitForSeconds(.5);
     var stickGrenade : GrenadeScript = weaponsInGame[8].gameObject.transform.GetComponent("GrenadeScript");
     stickGrenade.grenadeCount += amount;
     stickGrenade.DrawWeapon();
 }
 
 function OnGUI(){
     GUI.skin = mySkin;
     var style1 = mySkin.customStyles[0];
     if(showWepGui){
         if(!equipped){
             GUI.Label(Rect(Screen.width - (Screen.width/1.7),Screen.height - (Screen.height/1.4),800,100),"Press key << E >> to pickup " + weaponName + " for " + weaponPrice + " points", style1);
         }else{                                                                                                            //Press key << E >> to pickup " + /*weapIndex.*/weaponName/*.ToString()*/ + " for" /*weapIndex.*/weaponPrice/*.ToString()*/
             GUI.Label(Rect(Screen.width - (Screen.width/1.7),Screen.height - (Screen.height/1.4),800,100),"Weapon is already equipped");
         }
     }
 
     if(showAmmoGui){
         GUI.Label(Rect(Screen.width - (Screen.width/1.7),Screen.height - (Screen.height/1.4),800,200), notes + "\n" + textFromPickupScript, style1);
     }    
 }
 
 function DeselectWeapon(){
     //Dectivate all weapon
     for (var i : int = 0; i < weaponsInUse.length; i++){
         weaponsInUse[i].gameObject.SendMessage("Deselect", SendMessageOptions.DontRequireReceiver);
         var deactivate : Component[] = weaponsInUse[i].gameObject.GetComponentsInChildren(MonoBehaviour);
         for (var d in deactivate) {
             var d : MonoBehaviour = d as MonoBehaviour;
             if (d)
             d.enabled = false;
         }
         weaponsInUse[i].gameObject.SetActive(false);
     }
     Wait();
 }
 
 function Wait(){
     canSwitch = false;
     yield WaitForSeconds(switchWeaponTime);
     SelectWeapon(weaponToSelect);
     yield WaitForSeconds(switchWeaponTime);
     canSwitch = true;
 }
 
 function SelectWeapon (i : int) {
     //Activate selected weapon
     weaponsInUse[i].gameObject.SetActive(true);
     var activate : Component[] = weaponsInUse[i].gameObject.GetComponentsInChildren(MonoBehaviour);
     for (var a in activate) {
         var a : MonoBehaviour = a as MonoBehaviour;
         if (a)
         a.enabled = true;
     }
     weaponsInUse[i].gameObject.SendMessage("DrawWeapon", SendMessageOptions.DontRequireReceiver);
     var temp : WeaponIndex = weaponsInUse[i].gameObject.transform.GetComponent("WeaponIndex");
     weaponToDrop = temp.setWeapon;
 }
 
 function DropWeapon(index : int){
     
     for (var i : int = 0; i < worldModels.length; i++){
         if (i == index){
             var drop : Rigidbody = Instantiate(worldModels[i], dropPosition.transform.position, dropPosition.transform.rotation);
             drop.AddRelativeForce(0,50,Random.Range(100, 200));
         }
     }    
 }
 
 
     
 
 

So as you (should) see, not all of the script is on there. What I would like is for the script to recognize what weapon the Raycast is looking at and display the weapon's name and price. The problem is, right now the GUI only displays the Weapon with the first Weapon Index number (which is the Sniper, 1). Sorry if this is too long and belongs in the forums, but this is urgent! [1]: http://armedunity.com

Comment
Add comment · Show 8
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 getyour411 · Jan 26, 2014 at 02:19 AM 0
Share

All I see here is a line that would display "notes" and "textFromPickupScript" neither of which are shown in this code. How are they set, or is that your question?

avatar image HamCoder404 · Jan 26, 2014 at 03:01 AM 0
Share

@getyour411 +getyour411 I put in the entire script. please help!

avatar image HamCoder404 · Jan 26, 2014 at 03:03 AM 0
Share

I could give you the unityproject so you can review everything. I am just completely stuck.

avatar image getyour411 · Jan 26, 2014 at 03:11 AM 0
Share

Does this bit of code

  if (Physics.Raycast (position, direction, hit, distance, layer$$anonymous$$askWeapon.value)){
 //Debug.Log(hit.collider.gameObject.weapIndex.weaponPrice);
 weaponName = hit.collider.gameObject.name;

work - were you able to get the Raycast to send the weapon name to debug.log? If not, do you have your guns on the layer selected with layer$$anonymous$$askWeapon layer

avatar image HamCoder404 · Jan 26, 2014 at 03:19 AM 0
Share

For first question - No it doesn't. I have a feeling the problem has to do with the Raycast hit getting the GameObject's script. But I think youre onto something on the second question. I guess you suppose I am not on the layer$$anonymous$$askWeapon layer when I look at it?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weapon Selection Script 1 Answer

How to get raycast hit coordinates on Cardboard 0 Answers

GUI Follow RaycastHit 2 Answers

Dynamic Alpha Gradient Texture Collision Detection 0 Answers

Drag gui text and drop it on 3d object. 0 Answers


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