Collision Player with Player

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.

void OnCollisionEnter(Collision collision){
        if(collision.gameObject.tag == "Player"){
            curHealth--;
            if(curHealth==0){
                print ("Dead");
                curHealth=maxHealth;
            }
        }
    }

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.

Both players are running this script, right? :wink:

I created Network and Players using this tutorial: How to create an online multiplayer game with Unity - Paladin Studios
So everyplayer has same script.

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.

I was using networkView.isMine but it’s not working.

Kind of hard to guess things like this when you don’t show the actual code.

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.

It’s not fighing game or something like this. So I don’t want attacks.
Take a look at this script.

1898928–122383–Player.cs (2.14 KB)

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.

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.

Uhm. That script doesn’t have any of the collider logic in it? Also, please use code tags instead of attachments.

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:

gameObject.tag ="Hunter";  //you must have declared tag 'Hunter' in the tag manager)

//OR same with = "Player"; if they are in team B.

To swap a tag we can use your original script which is

void OnCollisionEnter(Collision collision){
        if(collision.gameObject.tag == "Hunter" && gameObject.tag == "Player"){ //so you are a player and collided with a hunter
            curHealth--;
            gameObject.tag = "Hunter"; // you become a hunter
            collision.gameObject.tag = "Player"; //use this if you want the old hunter to become a normal player

            if(curHealth==0){
                print ("Dead");
                curHealth=maxHealth;
            }
        }
    }

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.

1 Like

So I made two players. Hunter and Player.
First player who join will be Hunter.
Your script isn’t working like it should.

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.

Network script:

void OnConnectedToServer()
    {
        if(hunter)
            SpawnPlayerB();
        else
               SpawnPlayerA();
    }

SpawnPlayerB - spawn Hunter with tag Hunter
SpawnPlayerA - spawn Player with tag Player

private void SpawnPlayerA()
    {
        Network.Instantiate(playerPrefabA, Vector3.up * 5, Quaternion.identity, 0);
    }

    private void SpawnPlayerB()
    {
        Network.Instantiate(playerPrefabB, Vector3.up * 5, Quaternion.identity, 0);
        hunter = false;
    }

This spawn Players and Hunter.
Player has green color and Hunter has red color.

And here is your code inside Player script.

void OnCollisionEnter(Collision collision){
        if(gameObject.tag == "Player"){
            curHealth--;
            gameObject.tag = "Hunter";
            collision.gameObject.tag = "Player";
            if(curHealth==0){
                print ("Dead");
                curHealth=maxHealth;
            }
        }
    }

When Hunter touch Player, Player HP is less by one and Player tag change into Hunter. Hunter tag is same as was before.

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:

void OnCollisionEnter(Collision collision){
        if(gameObject.tag == "Finish"){
            Debug.Log ("Current Sphere tag:"+ gameObject.tag);
            Debug.Log ("Current Cube tag:"+ collision.gameObject.tag);

            gameObject.tag = "Respawn";
            collision.gameObject.tag = "Finish";

            Debug.Log ("After Sphere tag:"+ gameObject.tag);
            Debug.Log ("After Cube tag:"+ collision.gameObject.tag);

            Debug.Log ("gameObject.name = " +gameObject.name);
            Debug.Log( "Collision.gameObject.name = " + collision.gameObject.name );
        }
    }

and this is what the debug log returned: Screenshot by Lightshot

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.

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.

You could try fixing that by changing

if(gameObject.tag=="Player"){

to

if(gameObject.tag=="Player" && collision.gameObject.tag=="Hunter"){

to make sure you are a player and you collided WITH a hunter.