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.
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.
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.
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.
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.
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.
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.