- Home /
How to add Player health and ability to take damage from a cube?
I need to know how to make my player which is the Unity model called "RollerBall" take damage from a sliding cube which are called "cube" and "pillar" on touch and/or make it end the game with an option to restart.
I'm new to all this stuff and could really use the help. I appreciate your time if you can help me. Whatever it is you can help me with, i really need you and again appreciate your time. Im willing to collaborate with anyone to make this a reality and happen if needed.
Thanks, Rockstxr
Try looking at your question objectively. Breakdown the parts you want to accomplish and have a go at coding those smaller parts.
Then(if) you have any problems come here to ask for some help on a specific issue. Adding code and pictures to a question is also very helpful for people to understand your problem.
:)
I don't know how to do damage and health,that's all i need. I have a ball that is the player and cubes sliding at it that it needs to dodge or else it will get killed if it touches one. PLAYER NA$$anonymous$$E= RollerBall / ENE$$anonymous$$Y NA$$anonymous$$E: Cube / ENE$$anonymous$$Y NA$$anonymous$$E: Pillar
Answer by ForeignGod · Apr 09, 2015 at 10:17 AM
This isn't tested at all, it's just to show you how you can do it. Customize it to your needs
#pragma strict
var health: int;
function Start ()
{
health = 100;
}
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.name == "Pillar")
{
health-= 25;
}
if(health <= 0)
{
Destroy(gameObject);
}
}
Unity says: BCE0018: The name 'Int' does not denote a valid type ('not found'). Did you mean 'System.Int32'?
Answer by Kaz_Yamof · Apr 09, 2015 at 10:36 AM
Is impressive how people ask things so generic here. I mean... "How to receive damage?" You tell me, it's your game with your rules, you should be able to imagine them.
Try to be more specific: "How comunicate between two scripts?" or "How show the damage received on a lifebar?" or "How detect the end of a collision?"
This doubt is a very basic flaw in your coding skill, try start getting better on coding before learning Unity. Answer: You need to create your rule system. How much life the player has? How the damage is calculated: by weapon? by enemy? the player has an armor to defense himself? This is game design, and you need to have an ideia about how your game will be first. Supose that you have an Player with an armor, and an Enemy with an weapon, let's make the following:
Class name -> Player
Fields (define a value to each field as you want):
life -> int
maxLife -> int
minLife -> int
armor -> int
Class name -> Enemy
Fields
life -> int
maxLife -> int
minLife -> int
weapon -> damage
Now you will check how collision detection works. And in the collision method you subtract the life by enemy weapon plus armor.
This way your cube is receiving damage. But what goes when the life is zero? You will need to kill the cube, or do something else.
@daybson.paisante there is little point be patronising to the OP, simple directive answers to some helpful threads or tutorials would have been better.
However in your defence, you are correct, the question is very vague and to be honest should not have been accepted onto the UA. A moderator should have declined the post and directed the OP in his search.
:(