Quantcast
Channel: Answers by "Kleptomaniac"
Viewing all articles
Browse latest Browse all 90

Answer by Kleptomaniac

$
0
0
This script is kind of a mess. Here is it cleaned up and optimised: //Got rid of ApplyDamage() here, because it seemed to bear no purpose var maxHealth : int = 100; //Typed these, as dynamic typing could cost you precious execution speed var curHealth : int = 100; var playerDamage1 : int = 8; //Changed these to camelCase var playerDamage2 : int = 10; var playerDamage3 : int = 15; var playerDamage4 : int = 100; var enemyDamage : int = 10; var target : GameObject; var explosion : GameObject; var exp : GameObject; function Update() { //Got rid of ; here AddjustCurrentHealthEnemy(0); } function AddjustCurrentHealthEnemy(adjE) { curHealth += adjE; //I have no idea what adjE is, but I will leave it in there just in case (seems to be doing nothing) curHealth = Mathf.Clamp(curHealth, 0, maxHealth); //Simplified maxHealth = Mathf.Max(1, maxHealth); if(curHealth < 1) { //die var newExplosion : GameObject = Instantiate(explosion, transform.position, transform.rotation); var newExp: GameObject = Instantiate(exp, transform.position, transform.rotation); Destroy(gameObject); } } function OnCollisionEnter(collision : Collision) { //Don't need separate OnCollisionEnter functions if(collision.CompareTag("MG")){ curHealth -= playerDamage1; } else if(collision.CompareTag("PISTOL")){ curHealth -= playerDamage2; } else if(collision.CompareTag("SHOT")){ curHealth -= playerDamage3; } else if(collision.CompareTag("ROCKET")){ curHealth -= playerDamage4; } else if (collision.CompareTag("Player")) { //Make sure to tag player, "Player" collision.gameObject.GetComponent(PlayerHealth).AddjustCurrentHealthPlayer(-enemyDamage); var newExplosion: GameObject = Instantiate(explosion, collision.gameObject.transform.position, collision.gameObject.transform.rotation); Destroy(collision.gameObject); } } I'm not sure if it will work, as I haven't tested. Also, there seemed to be a lot of ambiguous stuff in there, so I commented all the stuff I changed. :) Hope that helps you, Klep ___ EDIT: Try it now. :)

Viewing all articles
Browse latest Browse all 90

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>