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 /
This question was closed Dec 29, 2014 at 08:56 PM by tanoshimi for the following reason:

Other. There is no technical question to be answered. UA is not a scriptwriting service.

avatar image
0
Question by $$anonymous$$ · Dec 24, 2014 at 05:37 AM · javascripthealthbar

C# to Java UI Health Bar

I have a C# script but i dont know C#. I need help to make it a javascript. I have no idea how to do it but i need the script urgently. It is from this tutorial: https://www.youtube.com/watch?v=NgftVg3idB4

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PlayerScript : MonoBehaviour
 {
     #region FIELDS
 
     /// <summary>
     /// The Player's movement speed
     /// </summary>
     public float speed;
 
     /// <summary>
     /// The players current health
     /// </summary>
     private int currentHealth;
 
     /// <summary>
     /// The player's max health
     /// </summary>
     public int maxHealth;
 
     /// <summary>
     /// The healt's transform, this is used for moving the object
     /// </summary>
     public RectTransform healthTransform;
 
     /// <summary>
     /// The health text
     /// </summary>
     public Text healthText;
 
     /// <summary>
     /// The health's image, this is used for color changing
     /// </summary>
     public Image visualHealth;
 
    /// <summary>
    /// The health's y pos
    /// </summary>
     private float cachedY;
 
     /// <summary>
     /// The minimum value of the health's x pos
     /// </summary>
     private float minXValue;
 
     /// <summary>
     /// The max value of the health's x pos
     /// </summary>
     private float maxXValue;
 
     /// <summary>
     /// The current xValue of the health
     /// </summary>
     private float currentXValue;
 
     /// <summary>
     /// How often can I take damage
     /// </summary>
     public float cooldown;
 
     /// <summary>
     /// Indicates if the we can take damage or not
     /// </summary>
     private bool onCD;
 
     #endregion
 
     #region PROPERTIES
 
     /// <summary>
     /// Property for accessing the players health
     /// </summary>
     public int Health
     {
         get { return currentHealth; }
         set
         {
             currentHealth = value;
             HandleHealthbar();
         }
     }
 
     #endregion
 
 
 
     // Use this for initialization
     void Start()
     {   
         //Sets all start values
         onCD = false;
         cachedY = healthTransform.position.y; //Caches the healthbar's start pos
         maxXValue = healthTransform.position.x; //The max value of the xPos is the start position
         minXValue = healthTransform.position.x - healthTransform.rect.width; //The minValue of the xPos is startPos - the width of the bar
         currentHealth = maxHealth; //Sets the current healt to the maxHealth
     }
 
     // Update is called once per frame
     void Update()
     {   
         //Makes sure that the player moves
         HandleMovement();
     }
 
     /// <summary>
     /// Handels the players movement
     /// </summary>
     private void HandleMovement()
     {   
         //Used for making the movement framerate independent
         float translation = speed * Time.deltaTime;
 
         transform.Translate(new Vector3(Input.GetAxis("Horizontal") * translation, 0, Input.GetAxis("Vertical") * translation));
     }
 
     /// <summary>
     /// Handles the healthbar by moving it and changing color
     /// </summary>
     private void HandleHealthbar()
     {   
         //Writes the current health in the text field
         healthText.text = "Health: " + currentHealth;
 
         //Maps the min and max position to the range between 0 and max health
         currentXValue = Map(currentHealth, 0, maxHealth, minXValue, maxXValue);
 
         //Sets the position of the health to simulate reduction of health
         healthTransform.position = new Vector3(currentXValue, cachedY);
 
         if (currentHealth > maxHealth / 2) //If we have more than 50% health we use the Green colors
         {
             visualHealth.color = new Color32((byte)Map(currentHealth, maxHealth / 2,maxHealth, 255, 0), 255, 0, 255);
         }
         else //If we have less than 50% health we use the red colors
         {
             visualHealth.color = new Color32(255, (byte)Map(currentHealth, 0, maxHealth / 2, 0, 255), 0, 255);
         }
     }
 
     void OnTriggerStay(Collider other)
     {
         if (other.tag == "Damage") //Used for simulating taking damage
         {
             if (!onCD && currentHealth > 1)
             {
                 StartCoroutine(CoolDownDmg()); //Makes sure that we can't take damage right away
                 Health -= 1; //Uses the Health Property to so that we recolor and rescale the health when we change it
             }
         }
         if (other.tag == "Health") //Used for simulating gaining health
         {
             if (!onCD && currentHealth < maxHealth)
             {
                 StartCoroutine(CoolDownDmg()); //Makes sure that we can't take damage right away
                 Health += 1; //Uses the Health Property to so that we recolor and rescale the health when we change it
             }
         }
     }
 
     /// <summary>
     /// Keeps track of the  damage CD
     /// </summary>
     /// <returns></returns>
     IEnumerator CoolDownDmg()
     {
         onCD = true; 
         yield return new WaitForSeconds(cooldown); //Waits a while before we are able to take dmg again
         onCD = false;
     }
 
     /// <summary>
     /// This method maps a range of number into another range
     /// </summary>
     /// <param name="x">The value to evaluate</param>
     /// <param name="in_min">The minimum value of the evaluated variable</param>
     /// <param name="in_max">The maximum value of the evaluated variable</param>
     /// <param name="out_min">The minum number we want to map to</param>
     /// <param name="out_max">The maximum number we want to map to</param>
     /// <returns></returns>
     public float Map(float x, float in_min, float in_max, float out_min, float out_max)
     {
         return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
     }
 }
 
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

1 Reply

  • Sort: 
avatar image
0

Answer by AcE_fLoOdEr · Dec 29, 2014 at 05:58 AM

There is not a lot of difference between C# and JS. You will just have to trial and error to get the syntax. I know that for variable declaration in JS it's as follows:

 private test : float;

so to translate C# variable declaration to JS all you have to do is switch around the order, example:

 // C#
 private float test = 0f;
 
 // JS
 private test : float = 0f;


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 $$anonymous$$ · Dec 29, 2014 at 08:26 PM 0
Share

I know that, but i have no clue about things like "$$anonymous$$ap" and having code outside of a function.

avatar image AcE_fLoOdEr · Dec 29, 2014 at 10:08 PM 0
Share

The map is a user created function. It's not a language based function, so you should be able to use it indifferently in both languages.

the only thing you will need to change for functions is as follows:

 public void <method name> (string test)
 {
     print(test);
 }
 
 // to
 
 function <method name> (test : string)
 {
    print (test);
 }


Follow this Question

Answers Answers and Comments

26 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

Related Questions

How Do I Make A Health Bar 6 Answers

Health Bar 0 Answers

My healtbar is not change when i look another enemy 0 Answers

Setting Scroll View Width GUILayout 1 Answer

Healt Bar Logic 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