Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 optimise · Oct 22, 2014 at 06:43 AM · 2dspritespriterendererspritesheetbar

How to truncate sprite without changing the scale of the sprite?

For now, I use transform.localScale to design a HP bar that will keep decreasing HP when player get hit but because of localScale issue I get the undesired result as image below. I dun want tat portion also changing. alt text

You will ask why dun just put a complete rectangle to do that but the UI layout is semi-transparent as image below. So, I need a way to truncate the sprite without modifying the scale of sprite.

alt text

My desired output is decrease the HP bar without changing the frame layout of the HP UI as the sample image below. alt text

Here is my code for modifying scale of the sprite:

 using UnityEngine;
 using System.Collections;
 
 public class HPBar : MonoBehaviour 
 {
     public float hp = 1800;
     public float damage = 510;
 
     float totalhp;
     float healthPercent = 100;
     SpriteRenderer hpBar;
     Vector3 hpScale;
 
     void Start()
     {
         hpBar = GetComponent<SpriteRenderer>();
         hpScale = hpBar.transform.localScale;
         totalhp = hp;
     }
 
     void Update()
     {
         // Mouse left click 
         if (Input.GetMouseButtonDown(0))
         {
             hp -= damage;
             if (hp > 0)
                 healthPercent = hp / totalhp;
             else
                 healthPercent = 0;
             hpBar.transform.localScale = new Vector3(hpScale.x * healthPercent, 1, 1);
         }
     }
 }
 


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 Redeemer86 · Oct 22, 2014 at 06:59 AM 0
Share

Use a different sprite bar( of the size of the bar below the main hp bar) object and super impose over the $$anonymous$$i bar sprite .. And then use a seperate code to manipulate the superimposed sprite.. Red

avatar image optimise · Oct 22, 2014 at 07:24 AM 0
Share

Not very understand wat u say. Can u explain in details? Thanks. Btw my desired output is decrease the HP bar without changing the frame layout of the HP UI.

1 Reply

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

Answer by Itaros · Oct 22, 2014 at 07:28 AM

Write a custom shader capable of truncating alpha by mask and supplied scaling factor(as float)

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 optimise · Oct 22, 2014 at 07:37 AM 0
Share

Why I get "$$anonymous$$aterial uses fixed function shader. It is not compatible with SpriteRenderer" error?

  Shader "Custom/$$anonymous$$askTest" {
  
      Properties
      {
          _$$anonymous$$ainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
          _Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
      }
   
      SubShader {  
          Tags {"Queue" = "Transparent"}
           Offset 0, -1
          Color$$anonymous$$ask 0
          ZWrite On
          Pass
          {
              AlphaTest Greater [_Cutoff]      
              SetTexture [_$$anonymous$$ainTex] {
                  combine texture * primary, texture
              }
          }
      }
  }
avatar image Itaros · Oct 22, 2014 at 07:49 AM 0
Share

You need proper vertex and fragment programs http://docs.unity3d.com/$$anonymous$$anual/SL-VertexFragmentShaderExamples.html

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

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

Related Questions

Sprite cutoff/altered in game view but not scene view 0 Answers

Change entire spriteSheet from animation tree 2 Answers

Issue with swapping Player sprite when health lowers - Not sure how to fix 1 Answer

Pixels from a sprite aren't the same size 0 Answers

Replace Sprite Sheet that's Already Animted? 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