- Home /
Turret Bullets hurt me
Hi im using Tornado Twins' YouTube series on how to make a turret shoot at me using this script:
var LookAtTarget : Transform;
var damp : float = 6.0;
var bullitPrefab : Transform;
var shotTime: float = 0;
var shotInterval: float = 2;
var speed : float = 1000.0;
function Update ()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
if (Time.time > shotTime){
Shoot();
shotTime = Time.time + shotInterval;
}
}
}
function Shoot()
{
var bullit = Instantiate(bullitPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward * speed);
}
and it works great but i want when the bullet hits me, for it to hurt me, or say if 5 bullets hit me, i respawn. Any idea how to do this? Thanks so much!
Answer by djm · Jun 20, 2012 at 03:32 AM
Try adding a health script to your player. When the player gets hit you take damage until you reach zero and at that point you will respawn.
var health : float = 100.0;
var damageRate:float = 3;
var startHealth:float;
function Awake()
{
startHealth = health;
}
function Update ()
{
if (health < 0)
{
Reset();
}
}
function OnGUI () {
GUI.backgroundColor = Color.yellow;
GUI.Label(Rect(300,10, 150, 30), "Player Health");
GUI.Box (Rect (300,40,150,30), "" + Mathf.Round(health));
}
function Reset(){
transform.position = Vector3(0,0,0);
health = startHealth;
print("your dead");
}
function OnTriggerEnter (theCollider : Collider) {
if(theCollider.tag == "enemy" )
{
health -=damageRate * Time.deltaTime;
}
}
I can't add tags in my Unity, its a bug i think is there another way?
@djm , for that health bar script do i need to head it with #pragma strict? :p
Hi JohnD, Not that I'm aware of, I'm testing it out on a game now and it works as expected, just need to add a tag for the "enemy".
But if there is a reason to disable the dynamic scripting, I guess you could try adding #pragma strict at the top. ;-)
Answer by lunaDirus · Jun 20, 2012 at 08:01 AM
i had trouble adding tags too. the problem is that the text field is the same color as the background of the tag manager
when you open the tag manager you have two columns the number and the tag name. place your mouse over the word water in the second column and move down until it is in line with user layer 8 in the first column if you there you will be able to type in a text field and make your own layer.
Your answer
Follow this Question
Related Questions
FPS spawn bullet 0 Answers
Enemy respawn once hit by bullet please help 1 Answer
Bullet Spawn 3 Answers
Character Re-spawn Problem 0 Answers
Bullet Ricochet Errors 0 Answers