- Home /
Question by
ringthane · Jan 10, 2015 at 01:08 PM ·
damageplayer-healthhealth
How to implement an attack in this script
So this script works but I dont know what to put to make them take health from me Here is my player health script
#pragma strict
var Health = 100;
function Update () {
if (Health <=0)
{
Dead();
}
}
function GiveDamage (damage : int)
{
Health -= damage;
}
function Dead()
{
Destroy (gameObject);
}
Here is my follow and attack script
#pragma strict
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 3;
var MinDist = 2;
var damage = 50;
function Start () {
}
function Update () {
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position)>= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <=MaxDist)
{
//Put some code to attack here
}
} }
Comment
Answer by hypnoticmeteor · Jan 10, 2015 at 01:16 PM
//In follow and attack add this code in the Start function
//add this line in follow and attack below damage
HealthScript HP;
//HealthScript is the name of the Script that holds the health.
function Start()
{
HP = Player.GetComponent("HealthScript");
}
//Put some code here
//if attack
HP.GiveDamage(//Damage Value)
Your answer
![](https://koobas.hobune.stream/wayback/20220613180017im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Health and damage reciever 1 Answer
Tunnel Vision Health System 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Health Bar Only For Falling Damage 2 Answers