• 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
-1
Question by Darkforce1980 · Oct 07, 2012 at 08:56 PM · healthbar

what wrong with my healthbarscript

I am trying to make a script for at healthbar. I have try to make 2 system 1 with 3 script(on 2 child og the same gameobject) and the other with 1 script(1 child og the same gameobject) ... the first one works fine... only error is that it all max hp gets overwritt by the last instantiate monster... the other one seens to have all the nummers rigtig ... hp , maxhp. but now the player cant hit it anymore.

// player script

var force : int; var allowfire = true; var fireRate = 0.1; var distance : float;// hvor lang kan man skyde

function Start () {

}

function Update () {

var hit : RaycastHit; var dir = transform.TransformDirection(Vector3.forward);//raycast lige frem

if(Input.GetMouseButtonDown(0) && allowfire == true){ Fire();

Debug.DrawRay(transform.position, dir * distance, Color.blue); Debug.Log("fire");

if (Physics.Raycast(transform.position, dir, hit, distance))//transform.position //if (Physics.Raycast(transform.position, dir, hit, distance))//transform.position { if (hit.collider.gameObject.tag == "target"){ hit.rigidbody.AddForceAtPosition(dir force,hit.point);// sender det som man rammer tilbage med en kraft :) hit.collider.SendMessageUpwards("ApplyDamage", 1.0, SendMessageOptions.DontRequireReceiver); Debug.Log("Hit"); } } } if(Input.GetMouseButton(1) && allowfire == true){ Fire(); Debug.DrawRay(transform.position, dir distance, Color.blue); Debug.Log("fire"); Debug.Log(transform.position);

if (Physics.Raycast(transform.parent.position, dir, hit, distance)) { if (hit.collider.gameObject.tag == "target"){ hit.rigidbody.AddForceAtPosition(dir * force,hit.point); hit.collider.SendMessageUpwards("ApplyDamage", 1.0, SendMessageOptions.DontRequireReceiver); Debug.Log("Hit"); } } }

}

function Fire() { allowfire = false; yield WaitForSeconds(fireRate); allowfire = true; }

//first script on healthbar var target : Transform; var rotationSpeed : int;

var myTransform : Transform; function Awake() { myTransform = transform; }

function Start () {

var go = GameObject.FindGameObjectWithTag("Player"); target = go.transform;

}

function Update () { Debug.DrawLine(target.transform.position, myTransform.position, Color.green);

//Look at player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

}

//secornd script on haelth bar

var scaleX : float; var hp : float; var maxHp : float;

function Awake() {

}

function Start () { scaleX = transform.localScale.x;

}

function Update () {

maxHp = modstanderhp.maxHP; hp = modstanderhp.HPpublic;

if(scaleX !=0 && hp != 0 && maxHp != 0){ transform.localScale.x = (scaleX * (hp/maxHp)); } } // script on piot to hit target var self : GameObject; var HP : int; //hvor mange HP har den var HPMax : int; var HPmin : int; var gotHit : boolean ; static var HPpublic; static var maxHP;

function Awake() { Respawn(); } function Start () {

}

function Update () { HPpublic = HP;

//sår bare zombien if(gotHit == true && HP >0 ){ gotHit = false; HP = HP - 1; }

if(gotHit == true && HP==0 ){ gotHit = false; ControllSpawn.currentzombie --; Destroy(self); } }

function Respawn(){ HP = Random.Range(HPmin,HPMax); this.maxHP = HP;

} function ApplyDamage (damage : float) { gotHit = true;

} //complet script in one var scaleX : float; var hp : float; var maxHp : float;

var self : GameObject; var HPMax : int; var HPmin : int; var gotHit : boolean ;

var target : Transform; var rotationSpeed : int;

var myTransform : Transform; function Awake() { myTransform = transform; maxHp = Random.Range(HPmin,HPMax); hp = maxHp; }

function Start () {

scaleX = transform.localScale.x; var go = GameObject.FindGameObjectWithTag("Player"); target = go.transform; }

function Update () {

collider.size = Vector3((scaleX 1 (maxHp/hp)), 1, 1); Debug.DrawLine(target.transform.position, myTransform.position, Color.green); //Look at player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

if(gotHit == true && hp > 1 ){ gotHit = false; hp --; } if(gotHit == true && hp == 1 ){ gotHit = false; ControllSpawn.currentzombie --; Destroy(self); } }

if(scaleX !=0 && hp != 0 && maxHp != 0){ transform.localScale.x = (scaleX * (hp/maxHp));

}

function ApplyDamage (damage : float) { gotHit =; true;

}

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 williampigmeu · Oct 07, 2012 at 09:00 PM 0
Share

Can I give you my healthbar script? It's very simple, more than this. xD

avatar image Darkforce1980 · Oct 08, 2012 at 04:16 AM 0
Share

ofcouse ... but I still would like to know what i need to do for this one to work :)

avatar image dannyskim · Oct 08, 2012 at 06:34 AM 3
Share

First thing is to format your code correctly.

avatar image Ludeme Games · Oct 08, 2012 at 06:45 AM 0
Share

There is a code tag in the question box that can help with what dannyskim said.

avatar image Darkforce1980 · Oct 09, 2012 at 03:32 PM 0
Share

So far I have not gotten much out of your help, no simple script ... and no real help to get my script to work anyone know it my script work? .... So far everything seems to work in the where everything is assembled except the last funtion can not be activated from the second script (player script)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Darkforce1980 · Oct 09, 2012 at 06:22 PM

`This script cant get hit by the player

 var scaleX : float;
 var hp : float;
 var maxHp : float;
 
 var self : GameObject;
 var HPMax : int;
 var HPmin : int;
 var gotHit : boolean ;
 
 var target : Transform;
 var rotationSpeed : int;
 
 
 var myTransform : Transform;
 function Awake()
 {
 myTransform = transform;
 maxHp = Random.Range(HPmin,HPMax);
 hp = maxHp;
 }
 
 function Start () {
 
 
 scaleX = transform.localScale.x;
 var go = GameObject.FindGameObjectWithTag("Player");
 target = go.transform;
 }
 
 
 function Update () {
 
 collider.size = Vector3((scaleX * 1 *(maxHp/hp)), 1, 1);
 Debug.DrawLine(target.transform.position, myTransform.position, Color.green);
 //Look at player
 myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
 
 if(gotHit == true && hp > 1 ){
 gotHit = false;
 hp --;
 }
 if(gotHit == true && hp == 1 ){
     gotHit = false;
     ControllSpawn.currentzombie --;
     Destroy(self);
     
     }
  }
 
 
 if(scaleX !=0 && hp != 0 && maxHp != 0){
 transform.localScale.x = (scaleX * (hp/maxHp));
 
 
 
 
  
  
 }
 
 function ApplyDamage (damage : float) {
 gotHit = true;
 
 }
 //playerscript for attakt
 var force : int;
 var allowfire = true;
 var fireRate = 0.1;
 var distance : float;// hvor lang kan man skyde
 
 function Start () {
 
 }
 
 
 function Update () {
 
 
 var hit : RaycastHit;
 var dir = transform.TransformDirection(Vector3.forward);//raycast lige frem
 
 
 if(Input.GetMouseButtonDown(0)  && allowfire == true){
 Fire();
 
 
 Debug.DrawRay(transform.position, dir * distance, Color.blue);
 Debug.Log("fire");
 
 
 if (Physics.Raycast(transform.position, dir, hit, distance))//transform.position
 //if (Physics.Raycast(transform.position, dir, hit, distance))//transform.position
     {
     
     if (hit.collider.gameObject.tag == "target"){
     hit.rigidbody.AddForceAtPosition(dir * force,hit.point);// sender det som man rammer tilbage med en kraft :)
     hit.collider.SendMessageUpwards("ApplyDamage", 1.0, SendMessageOptions.DontRequireReceiver);
     Debug.Log("Hit");
     }
     }
     }
     
     if(Input.GetMouseButton(1)  && allowfire == true){
     Fire();
     Debug.DrawRay(transform.position, dir * distance, Color.blue);
     Debug.Log("fire");
     Debug.Log(transform.position);
 
 if (Physics.Raycast(transform.parent.position, dir, hit, distance))
     {
     
     if (hit.collider.gameObject.tag == "target"){
     hit.rigidbody.AddForceAtPosition(dir * force,hit.point);
     hit.collider.SendMessageUpwards("ApplyDamage", 1.0, SendMessageOptions.DontRequireReceiver);
     Debug.Log("Hit");
     }
     }
     }
     
 
 
     }
     
 
 
 function Fire()
 {
     allowfire = false;
     yield WaitForSeconds(fireRate);
     allowfire = 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

How to make an healthbar 1 Answer

Draw orthographic visuals (e.g. health bar) in perspective scene? 4 Answers

Show changes in healthbar 1 Answer

Gui box size calculations 0-1 3 Answers

Power up timer... 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