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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by williamalexandre · Aug 28, 2012 at 06:40 PM · javascriptdamageconvertattackhealth

I need help with my health script

hi, i was trying to convert these scripts c # to javascript, but they are not working correctly.


playerHealth javascript:

     public var maxHealth : int = 100;
     public var curHealth : int = 100;
     public var healthBarLenght : float;
     
     // Use this for initialization
     function Start () {
         healthBarLenght = Screen.width / 2;
     
     }
     
     // Update is called once per frame
     function Update () {
         addjustCurrentHealth(0);
     }
     
     function OnGUI () {
         
         
         GUI.Button(new Rect(10, 40, healthBarLenght, 20), curHealth + "/" + maxHealth);
         
 }
     function addjustCurrentHealth(adj : int) {
         curHealth += adj;
         
         if(curHealth < 0)
             curHealth = 0;
         
         if(curHealth > maxHealth)
             curHealth = maxHealth;
         
         if(maxHealth < 1)
             maxHealth = 1;
             
             healthBarLenght = Screen.width / 2 * curHealth/(maxHealth);
             }
             
             



playerAttack javascript:

      var target : GameObject;
      var attackTimer : float;
      var coolDown : float;
     
     function Start () {
         attackTimer = 0;
         coolDown = 0.45f;
     
     }
     
     
     function Update() {
         if(attackTimer > 0)
         attackTimer -= Time.deltaTime;
         
         if(attackTimer < 0)
         attackTimer = 0;
         
         if(Input.GetButton("Fire1")) {
         if(attackTimer == 0)
             attackTimer = coolDown;
         
         
         }
     }
     
     private function attack () {
         var distance : float = Vector3.Distance(target.transform.position, transform.position);
         var dir : Vector3 = (target.transform.position - transform.position).normalized;
         var direction : float = Vector3.Dot(dir, transform.forward);
         
         
         if(distance < 0.8) {
             if(direction > 0) {
         var eh : enemyVida = (enemyVida).target.GetComponent("enemyVida");
         eh.addjustCurrentHealth(-10);
         }
         }
         }
         
         
   


someone could help me.

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

2 Replies

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

Answer by Seth-Bergman · Aug 28, 2012 at 06:49 PM

 var eh : enemyVida = (enemyVida).target.GetComponent("enemyVida");

change it to

 var eh : enemyVida = target.GetComponent(enemyVida);

otherwise this looks ok I think...

but I don't see why you are using GUI.Button, maybe GUI.Label would be more appropriate..

 GUI.Label(Rect(10, 40, healthBarLenght, 20), curHealth + "/" + maxHealth);

EDIT:

add this line:

  private function attack () {
        var distance : float = Vector3.Distance(target.transform.position, transform.position);
        Debug.Log(distance);
        ...etc

also btw, get rid of this:

 function Update () {
    addjustCurrentHealth(0);
 }

it's pointless..

Make sure you are calling the attack function (it is not being called from anything here that I can see).. If the Debug.Log never shows, that's the problem

Comment
Add comment · Show 3 · 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 williamalexandre · Aug 28, 2012 at 07:42 PM 0
Share

thanks for the help, but I tried to use your code more failed, does not lower the health.

avatar image Seth-Bergman · Aug 28, 2012 at 07:55 PM 0
Share

are you getting any errors? Try adding some debug logs to see if your functions are being entered, and also to see what "distance" is, since it may never be below 0.8. Is the attack function being entered?

just add this line:

  private function attack () {
        var distance : float = Vector3.Distance(target.transform.position, transform.position);
        Debug.Log(distance);
        ...etc

also btw, get rid of this:

 function Update () {
    addjustCurrentHealth(0);
 }

it's pointless..

Oh, and are you even calling the "attack" function? It doesn't appear so here.. You need to call that function somewhere..

also, is target set to the right object (the one with enemyVida on it)?

avatar image williamalexandre · Aug 28, 2012 at 08:53 PM 0
Share

thanks, the script is working now. :D

avatar image
0

Answer by Panik.Studios · Aug 29, 2012 at 12:45 AM

by not working correctly is it that your health always stays at 100? Or that the script has errors.

The way it's written now is saying curhealth = 100;

I would do var curhealth : int; and then after function start() curhealth = 100;

Whats the exact problem? Or, am I reading wrong?

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

rigidbody enemy goes only forward, no damage delay. 2 Answers

Player take damage on collision with AI 1 Answer

The enemy don´t lose health, but why??? 0 Answers

Melee Damage script by collision 2 Answers

Punching - what would be the better way? (in javascript) 2 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