- Home /
Hurt Player if close to Enemy! Need Help
Im creating a little Horror Game in Unity! I donw all Basics (First Person Controller, Flashlight, World) and a Player-Following Enemy! Ive done the Scipt, that the Enemy follows the Player.
But how can i do, when the Player comes into Enemys Capsule Collider, that he loses Health?
IM NOT TRING TO RE CREATE SLENDER! THIS IS MY OWN IDEA!
Thanks
Answer by Kiloblargh · Feb 27, 2013 at 06:15 PM
Have you read at any of the tutorials, or looked at the scripts on the built-in Angry Bots demo game?
Answer by KoshX87 · Feb 27, 2013 at 07:19 PM
Ok. You will need to make a sphere or a capsule or whatever collision zone you need (eg. if you want to make the enemies hurt you from a distance of 5 units, you make a 5 unit radius sphere.) Note: If you want the collision area to be normal, just use normal OnCollisionEnter. Using OnCollisionEnter() is straight forward and similar to what i will explain but, the if you use the capsule collider, the enemy be able to go through it if you make it bigger than your actual player.
So you make an empty Game object from the top menu and dag this onto your player object. Add a sphere collider of whatever radius you need to this new GameObject and rename the object to something intrinsic or obvious. Then select isTrigger and make a new script which will be placed on this GameObejct. If you haven't already look at the OnTriggerEnter(other:Collider) Function on Unity Scripting Reference.(Note: "other" can be renamed if you want but then you have to use that new name instead i.e destroy(whateverName.gameObject))
Once you have done that:
At the start of the code make a new variable of type GameObject (e.g. var self:gameObject). You will end up dragging that gameobject with the trigger on it.
if (other.gameObject.tag == "Zombie"){ playerHealth -= 20; //decrease playerHealth var by 20 if zombie enters the trigger area. }
So all you have to do is put your enemy's tag inside the quotation marks and if you have changed other at the begining of the function, use the new variable instead of other in the code i put above.
That should just about do it, but if you don't understand still please say the new or existing problem and what you want it to do instead
this is my code
if(Vector3.Distance(transform.position, thePlayer.position) <= 5)
{
health -= damage * Time.deltaTime;
}
}
if ( health == 0.0 )
{
health == 0.0;
Application.LoadLevel( "gameover" );
}
Now the Problem is, if the Player Health is 0, he doenst loads the Game Over Level, he counts to negative (-1, -2, ...)
still doesnt works
if (health <= 0.0) {
health == 0.0;
Application.LoadLevel( "gameover" );
}
go to you're build settings and make sure the checkbox next to the gameover scene is ticked. It says in the script reference for Application.LoadLevel right here
Answer by MegaStevenLP · Feb 27, 2013 at 10:44 PM
Created my own Script, thanks
Don't put comments as answers. If you solved the problem yourself, either delete or close the question, or post your script that you came up with and accept your own answer.
Your answer
Follow this Question
Related Questions
Change Health of a duplicated enemeis 1 Answer
Health to come above enemy when clicked 1 Answer
Problem with 2 scripts communicating 0 Answers
Taking a hit 3 Answers
How to create Enemy Health Bar ? 4 Answers