Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Collision with enemy = lose health

Discussion in 'Scripting' started by EricTheCoolDude, Apr 6, 2011.

  1. EricTheCoolDude

    EricTheCoolDude

    Joined:
    Feb 21, 2011
    Posts:
    164
    So yeah, I'm kinda having a problem with this. I have a player (First person controller) and and enemies that run at the player (Rigidbody and stuff). What I need is to remove a number from the global health var and throw the player backwards a little bit when an enemy hits the player. I've been trying onCollisionEnter and such but it doesn't work and I can't seem to specify that the player should only lose health then the ENEMY hits the PLAYER and nothing else. Any suggestions?

    Also, I have pickups but right now they can be picked up by anything that collides with them which means the enemies keep giving me more ammo and such. How do I say exactly which objects CAN collide?
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,092
    OnCollisionEnter() won't work if you put it in a script on the FPS Controller, because its not a rigidbody; but it will work if you put it in a script on the enemy. Just tag your FPS Controller "Player", and the put a script on the Enemy that says:

    Code (csharp):
    1.  
    2. function OnCollisionEnter(collision : Collision){
    3.     if(collision.CompareTag("Player")){
    4.         //Reduce global health var
    5.     }
    6. }
    7.  
     
    Last edited: Apr 6, 2011
  3. EricTheCoolDude

    EricTheCoolDude

    Joined:
    Feb 21, 2011
    Posts:
    164
    Thanks for the speedy reply! I'll try that. Any idea how I would push the player in the enemys direction?
     
  4. EricTheCoolDude

    EricTheCoolDude

    Joined:
    Feb 21, 2011
    Posts:
    164
    Hmm. I get an error. 'CompareTag' is not a member of 'UnityEngine.Collision'.
     
  5. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,092
    Sure, what I would do is convert the enemy's forward vector to world space, and then send that vector to the player so he can move in that direction. It'd be a little weird with a CharacterController because I don't know what happens when two move commands are happening at the same time, and instead of applying force you're going to have to move the player over a period of time (so we'll say, a knockback will move the player in the direction that the enemy sent him for 0.25 second at 15 units per second).

    I think i would do something like this (not tested):

    Code (csharp):
    1.  
    2. //this script goes on the enemies
    3. function OnCollisionEnter(collision : Collision){
    4.     if(collision.gameObject.CompareTag("Player")){
    5.         //Reduce global health var here
    6.         //now we'll knock the player back
    7.         var myWorldForward : Vector3 = transform.TransformDirection(Vector3.forward); // convert my local forward to world space
    8.         collision.gameObject.SendMessage("Knockback", myWorldForward);
    9.     }
    10. }
    11.  
    Code (csharp):
    1.  
    2. //this script goes on the player
    3. public var speed : float = 15.0;
    4. public var duration : float = 0.25;
    5.  
    6. private var controller : CharacterController;
    7.  
    8. function Start(){
    9.     controller = GetComponent(CharacterController);
    10. }
    11.  
    12. function Knockback(direction : Vector3){
    13.     var startTime = Time.time;
    14.     while(Time.time < (startTime + duration)){
    15.         controller.SimpleMove(direction*speed);
    16.         yield;
    17.     }
    18. }
    19.  
     
    Last edited: Apr 6, 2011
  6. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,092
    hm... try collision.gameObject.CompareTag(). I updated my most recent response to reflect that.
     
    Last edited: Apr 6, 2011
  7. EricTheCoolDude

    EricTheCoolDude

    Joined:
    Feb 21, 2011
    Posts:
    164
    Wow, thanks! It all works perfectly! I'll be sure to give you credit for your help :)
     
  8. Hjalpmovie

    Hjalpmovie

    Joined:
    Mar 24, 2013
    Posts:
    2
    What do you guys mean by "//Reduce global health var here"??? :)
     
  9. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Please don't necro bump a old thread. This thread is nearly 2 years old.

    Make your own question if you have problems.
     
  10. Hjalpmovie

    Hjalpmovie

    Joined:
    Mar 24, 2013
    Posts:
    2
    I did, but everyone send me this page! :(