Search Unity

Collision Damage

Discussion in 'Scripting' started by Aimlessone, Jan 5, 2016.

  1. Aimlessone

    Aimlessone

    Joined:
    Jun 26, 2015
    Posts:
    46
    I'm trying to get my PlayerHealth script to calculate damage when colliding with a laser shot. I've got the health bar to appear and values seems to apply correctly, but my script isn't detecting the collision nor subtracting health when the collision happens (obviously because it's not detecting the collision). I actually want the lasershot to be a physical thing in the game world, so I don't think using it as a trigger is the best solution. Anyway here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerHealth : MonoBehaviour {
    5.  
    6.     public int maxHealth = 5;
    7.     public int curHealth = 5;
    8.     public int laserDamage = 1;
    9.  
    10.     void OnGUI()
    11.     {
    12.         GUI.Box(new Rect(5, 5, Screen.width / 3 / (maxHealth / curHealth), 20), curHealth + "/" + maxHealth);
    13.     }
    14.  
    15.     void onCollisionEnter (Collision collision)
    16.     {
    17.         if (collision.collider.gameObject.tag == "Laser")
    18.         {
    19.             Debug.Log ("Hit!");
    20.             curHealth = curHealth - laserDamage;
    21.         }
    22.  
    23.     }
    24.  
    25. }
    26.  
    27.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Aimlessone

    Aimlessone

    Joined:
    Jun 26, 2015
    Posts:
    46
    Doh! Well that fixed it! Thanks mate! Seriously hate being a noob at this.

    I'll check out the new UI system here shortly.

    Thanks again.
     
  4. djgriff

    djgriff

    Joined:
    May 29, 2014
    Posts:
    279
  5. Aimlessone

    Aimlessone

    Joined:
    Jun 26, 2015
    Posts:
    46
    djgriff,

    Thanks for the link. I've actually done all but two of the Unity tutorials including the survival shooter. But they were using triggers as colliders from what I remember and can see within the scripts. Also, maybe it is just me but I have a really hard time going back to those tutorials and finding the parts I need to refresh my memory.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you may also find that trigger vs collider is moot for fast projectiles... you can get into a situation where the object is moving faster than the physics engine can work out what it has hit so it passes through things when it shouldn't. In which case you'll need to switch to raycasts. Seb's done a really nice series which does that: https://www.youtube.com/playlist?list=PLFt_AvWsXl0ctd4dgE1F8g3uec4zKNRV0 (think it's no.2 gun in particular)
     
  7. Aimlessone

    Aimlessone

    Joined:
    Jun 26, 2015
    Posts:
    46
    LeftyRighty,

    Thanks for the link, I'll add it to the many things I need to research and learn. I'm still new but I constantly hear about raycasts, so I'll definitely need to look into them sooner rather than later.