Note that there are some explanatory texts on larger screens.

plurals
  1. POCollision with enemy to inflict damage
    text
    copied!<p>I'm having some problems with unity. I'm trying to make it so that if an enemy collides with the player, the player looses a health point. My code is in C# it goes like:</p> <p>before you look at the code, I wanted to say that the enemies are rigidbodies so that the object bullets can effect them. I made an extra capsule to be apart of the player body that can be a rigid body so that the code can detect collision. Do you think that would work? I'm unsure if it's easier for a rigid body to detect another rigidbody collision or if it doesn't care.</p> <pre><code>//basic stuff// public class playerhealth : MonoBehaviour { private int curHealth; private int playerLives; public GUIText winText; public GUIText healthText; public GUIText livesText; void Start (){ curHealth = 3; playerLives = 3; SetHealthText (); SetLivesText (); winText.text = ""; } void FixedUpdate()//where physics codes go { } //HERE'S WHERE THE COLLISIONS STUFF IS void OnCollisionEnter(Collider rigidbody){ if(rigidbody.gameObject.tag == "Enemy"){ curHealth = curHealth - 1; SetHealthText (); } if(rigidbody.gameObject.tag == "reloader"){ playerLives = playerLives - 1; SetLivesText (); } } //setting GUI TEXT and reloading level void SetHealthText (){ healthText.text = "Health Points: " + curHealth.ToString(); if(curHealth &lt;=0) { Application.LoadLevel ("shootingworld"); playerLives = playerLives - 1; } if(curHealth &gt;= 10){ playerLives+= 1; } } void SetLivesText (){ livesText.text = "Lives: " + playerLives.ToString(); if (playerLives &lt;= 0){ winText.text = "GAME OVER"; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload