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
2
Question by siOnzee · Oct 02, 2012 at 04:23 PM · weapondisplayhudammo

Hud on Weapon

I need create hud on weapon (display ammo) Can you help me how to do it ?

Example:

alt text

Comment
Add comment · Show 2
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 Kaili-chan · Oct 02, 2012 at 04:59 PM 0
Share

With render to texture you can get that kind of effect but you need pro for that to work. I'd be interested in seeing if anyone has a solution for Unity free.

avatar image Kaili-chan · Oct 02, 2012 at 05:01 PM 0
Share

Using a 3d text object attached to the weapon you could get a semi similar effect. For boarders use a regular texture placed around it.

1 Reply

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

Answer by Kaili-chan · Oct 02, 2012 at 10:29 PM

Option 1:

Use this UnityWiki Script you can create a sprite sheet attached to an object to loop through your sprite sheet.

The script was all messed up when I went to that link I reformatted it and cleaned it up for easier reading.

You could hook into this script with a trigger to update the index, set it manually or do anything else you wanted to do

 public class AnimatedTextureExtendedUV : MonoBehaviour {
     //vars for the whole sheet 
     public int colCount = 4; 
     public int rowCount = 4;//vars for animation 
     public int rowNumber = 0; //Zero Indexed
     public int colNumber = 0; //Zero Indexed 
     public int totalCells = 4; 
     public int fps = 10;
     //Maybe this should be a private var
     private Vector2 offset;
     //Update 
     void Update () { SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps); }
     
     //SetSpriteAnimation 
     void SetSpriteAnimation(int colCount ,int rowCount ,int rowNumber ,int colNumber,int totalCells,int fps ){
         // Calculate index
         int index  = (int)(Time.time * fps);
         // Repeat when exhausting all cells
         index = index % totalCells;
 
         // Size of every cell
         float sizeX = 1.0f / colCount;
         float sizeY = 1.0f / rowCount;
         Vector2 size =  new Vector2(sizeX,sizeY);
 
         // split into horizontal and vertical index
         var uIndex = index % colCount;
         var vIndex = index / colCount;
 
         // build offset
         // v coordinate is the bottom of the image in opengl so we need to invert.
         float offsetX = (uIndex+colNumber) * size.x;
         float offsetY = (1.0f - size.y) - (vIndex + rowNumber) * size.y;
         Vector2 offset = new Vector2(offsetX,offsetY);
 
         renderer.material.SetTextureOffset ("_MainTex", offset);
         renderer.material.SetTextureScale  ("_MainTex", size);
     }
 } 


Options 2: Create an empty game object. Add a mesh. Texture the mesh an appropriate background texture (in your image above it would be the blue backer). Add a 3d text object for current bullet count, and a 3d text for out of bullet count. Create a prefab and attach this script to your ammo counter object.

Extended Node: You might need this shader from the unity wiki Shader script

 public class AmmoCounterScript : MonoBehaviour {
     
     public TextMesh AmmoLeftMesh;
     public TextMesh AmmoInMagMesh; // Include any other text you want in the sam fashion
     
     public int AmmoLeft = 0;
     public int AmmoInMag = 0;
     
     public bool DidChangeAmmoCount = false;
     
     void Update () { 
     // Not sure if this is really an optimization but it doesn't hurt
         if (DidChangeAmmoCount) {
             AmmoLeftMesh.Text = "" + AmmoLeft;
             AmmoInMagMesh.Text = "" + AmmoInMag;
             DidChangeAmmoCount = false;
         }
     }
     
     public void ChangeAmmoLeft(int newValue) {
         AmmoLeft = newValue;
         DidChangeAmmoCount = true;
     }
     
     public void ChangeAmmoInMag(int newValue) {
         AmmoInMag = newValue;
         DidChangeAmmoCount = true;
     }
     
     public void DecrementAmmLeft() {
         AmmoLeft--;
         DidChangeAmmoCount = true;
     }
     
 } 

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

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

Reload manually in raycast? 1 Answer

Display timer when power up is in use (C#) 1 Answer

How do I get my Ammo Hud on the bottom right of the screen? 1 Answer

Ammo Counting, 1 Answer

Total Bullets to weapon reload? Help 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