Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Collision Player with Player

Discussion in 'Scripting' started by Oen44, Dec 26, 2014.

  1. Oen44

    Oen44

    Joined:
    Feb 13, 2014
    Posts:
    24
    Hello,

    So I have script called Player. What I want to do is that when Player touch other Player then his health will be less by one. I have that code but whenever i touch other Player, my health and his is going down.
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision){
    2.         if(collision.gameObject.tag == "Player"){
    3.             curHealth--;
    4.             if(curHealth==0){
    5.                 print ("Dead");
    6.                 curHealth=maxHealth;
    7.             }
    8.         }
    9.     }
     
  2. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Is your "Player" script attached to both of your colliding objects and are they both called "Player"?

    And if this is the case, how do you determine which one will lose health? At the moment your script just tells that when you collide with a player your health will go down and if both have this script then they both will lose health.
     
  3. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Both players are running this script, right? ;)
     
  4. Oen44

    Oen44

    Joined:
    Feb 13, 2014
    Posts:
    24
  5. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    You should disable all (...) the scripts on your player prefab and only enable them if the player you are instantiating is yours.

    Or - instead of disabling - check if you are the player when stuff is happening.
     
  6. Oen44

    Oen44

    Joined:
    Feb 13, 2014
    Posts:
    24
    I was using networkView.isMine but it's not working.
     
  7. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Kind of hard to guess things like this when you don't show the actual code.
     
  8. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    So if you are colliding with OTHER playing players(right?), how do you know who hit who? You should use some method for something like melee attacking, like raycast.

    However you could replace curHealth--; with collision.curHealth--; to lower your target's health, however this still applies to both as you both have the same script so there's no point in doing it.
     
  9. Oen44

    Oen44

    Joined:
    Feb 13, 2014
    Posts:
    24
    It's not fighing game or something like this. So I don't want attacks.
    Take a look at this script.
     

    Attached Files:

  10. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    The question is, when two players collide how do you know which one of them "wanted" to collide? If this is kinda a tagging game where player A hunts player B then you could give players who hunt other players a tag like "Hunters" or something like that and when they collide see which one has that tag and which one doesn't. However if they both want to collide with each other then there's really no easy way of telling which one wanted to collide in the first place.
     
  11. Oen44

    Oen44

    Joined:
    Feb 13, 2014
    Posts:
    24
    For example.
    -4 players
    -1 player is catching others
    -when player catch someone then catched player lose one HP and he is catching now

    So, how to randomaly spawn player with tag Catcher and others with tag Player and when Catcher collide with Player their Tag swap.
     
  12. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Uhm. That script doesn't have any of the collider logic in it? Also, please use code tags instead of attachments.
     
  13. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    To spawn people randomly with certain tags, you could create two teams and everyone in team A which is the catchers team, can be given tag "Hunter" and everyone in team B could be "Player".

    To assign a tag to a player you check, in which team the player is and do the following:
    Code (CSharp):
    1.  
    2. gameObject.tag ="Hunter";  //you must have declared tag 'Hunter' in the tag manager)
    3.  
    4. //OR same with = "Player"; if they are in team B.
    5.  
    To swap a tag we can use your original script which is

    Code (CSharp):
    1.  
    2. void OnCollisionEnter(Collision collision){
    3.         if(collision.gameObject.tag == "Hunter" && gameObject.tag == "Player"){ //so you are a player and collided with a hunter
    4.             curHealth--;
    5.             gameObject.tag = "Hunter"; // you become a hunter
    6.             collision.gameObject.tag = "Player"; //use this if you want the old hunter to become a normal player
    7.  
    8.             if(curHealth==0){
    9.                 print ("Dead");
    10.                 curHealth=maxHealth;
    11.             }
    12.         }
    13.     }
    14.  
    15.  
    You probably have different models for both teams, so instead of doing the tag thing, you could also assign a prefab(player model) for both teams with tags included.

    I want to point out that I haven't tested this and i'm not 100% sure if it will work but it should at least give you some clue what to do next.
     
    Last edited: Dec 26, 2014
    Oen44 likes this.
  14. Oen44

    Oen44

    Joined:
    Feb 13, 2014
    Posts:
    24
    So I made two players. Hunter and Player.
    First player who join will be Hunter.
    Your script isn't working like it should.
     
  15. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    It's really difficult to help without seeing the errors/what you did but hopefully this will get you going.
    There are some great tutorials for doing stuff like this, google is your friend.
     
  16. Oen44

    Oen44

    Joined:
    Feb 13, 2014
    Posts:
    24
    Network script:
    Code (CSharp):
    1. void OnConnectedToServer()
    2.     {
    3.         if(hunter)
    4.             SpawnPlayerB();
    5.         else
    6.                SpawnPlayerA();
    7.     }
    SpawnPlayerB - spawn Hunter with tag Hunter
    SpawnPlayerA - spawn Player with tag Player

    Code (CSharp):
    1. private void SpawnPlayerA()
    2.     {
    3.         Network.Instantiate(playerPrefabA, Vector3.up * 5, Quaternion.identity, 0);
    4.     }
    5.  
    6.     private void SpawnPlayerB()
    7.     {
    8.         Network.Instantiate(playerPrefabB, Vector3.up * 5, Quaternion.identity, 0);
    9.         hunter = false;
    10.     }
    This spawn Players and Hunter.
    Player has green color and Hunter has red color.

    And here is your code inside Player script.
    Code (CSharp):
    1. void OnCollisionEnter(Collision collision){
    2.         if(gameObject.tag == "Player"){
    3.             curHealth--;
    4.             gameObject.tag = "Hunter";
    5.             collision.gameObject.tag = "Player";
    6.             if(curHealth==0){
    7.                 print ("Dead");
    8.                 curHealth=maxHealth;
    9.             }
    10.         }
    11.     }
    When Hunter touch Player, Player HP is less by one and Player tag change into Hunter. Hunter tag is same as was before.
     
  17. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    Code (CSharp):
    1. collision.gameObject.tag = "Player";
    This line should change hunter's tag to "Player" and it should work. I tried this myself in an empty scene and it worked perfectly.

    I created a cube and a sphere which both had script "test" attached to them, they both also had a rigidbody and a collider.

    Sphere had a tag "Finish" and cube had none.

    "test" script had this function in it:

    Code (CSharp):
    1.  
    2. void OnCollisionEnter(Collision collision){
    3.         if(gameObject.tag == "Finish"){
    4.             Debug.Log ("Current Sphere tag:"+ gameObject.tag);
    5.             Debug.Log ("Current Cube tag:"+ collision.gameObject.tag);
    6.  
    7.             gameObject.tag = "Respawn";
    8.             collision.gameObject.tag = "Finish";
    9.  
    10.             Debug.Log ("After Sphere tag:"+ gameObject.tag);
    11.             Debug.Log ("After Cube tag:"+ collision.gameObject.tag);
    12.  
    13.             Debug.Log ("gameObject.name = " +gameObject.name);
    14.             Debug.Log( "Collision.gameObject.name = " + collision.gameObject.name );
    15.         }
    16.     }
    17.  
    18.  
    and this is what the debug log returned: http://prntscr.com/5litd5

    As you can see, sphere tag changed from "Finish" to "Respawn" and cube tag changed from none to "Finish".

    NOTE: I used tags Respawn and Finish only because unity has them already declared inside the tag inspector, it should work with Hunter and Player assuming you have declared them there.

    If you still cannot get this work inside your project or inside an empty scene then I have no idea why it doesn't work as it works for me.
     
  18. Oen44

    Oen44

    Joined:
    Feb 13, 2014
    Posts:
    24
    Weird. When I'm looking in inspector, tags aren't changing but debug log shows that they changed.

    Ohh, I just noticed that plane tag is changing too! So this is maybe why it's not showing because cubes are always on plane so it's like tag change loop.
     
  19. GNGification

    GNGification

    Joined:
    Oct 24, 2013
    Posts:
    59
    You could try fixing that by changing
    Code (CSharp):
    1. if(gameObject.tag=="Player"){
    to
    Code (CSharp):
    1. if(gameObject.tag=="Player" && collision.gameObject.tag=="Hunter"){
    to make sure you are a player and you collided WITH a hunter.