Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Turret Death Script

Discussion in 'Scripting' started by Jack.H, Apr 28, 2012.

  1. Jack.H

    Jack.H

    Joined:
    Nov 29, 2011
    Posts:
    48
    I currently have a turret in game which fires small boxes at the player when the player is within a certain range of the turret. However I am stuck for what to do next script-wise. I need the bullet to kill the player on contact and load the level again. The script I have for my turret is as follows:
    Code (csharp):
    1.  
    2. private var target : Transform;
    3.  
    4.  
    5. function OnTriggerEnter(otherCollider : Collider) {
    6. if (otherCollider.CompareTag("Player"))
    7. {
    8. target = otherCollider.transform;
    9. Fire();
    10. }
    11. }
    12.  
    13. function OnTriggerExit(otherCollider : Collider) {
    14. if (otherCollider.CompareTag("Player"))
    15. {
    16. target = null;
    17. StopCoroutine("Fire"); // aborts the currently running Fire() coroutine
    18. }
    19. }
    20.  
    21. function Fire()
    22. {
    23. while (target != null)
    24. {
    25. var nextFire = Time.time + 2;
    26. while (Time.time < nextFire)
    27. {
    28. transform.LookAt(target);
    29. yield WaitForEndOfFrame();
    30. }
    31.  
    32. // fire!
    33. var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
    34. bullet.velocity = transform.forward * bulletSpeed;
    35. }
    36. }
    37.  
    What would I need to add to this code or the box in order to make the level re-load from the beginning? I'm aware of the application.LoadLevel(1) code, but I'm not sure how I would go about triggering this code when the box or bullet hits the player. For reference I am using a default third person controller.

    Thanks in advance :)
     
  2. Kaze_Senshi

    Kaze_Senshi

    Joined:
    Feb 19, 2012
    Posts:
    243
    For me you need to add a new function in the player instead to add it too the turret. In this script in the player, you can check if your player collides with the bullet, subtract the health, kill the player. Also this way you can activate some boolean to ignore the player commands while the death animation is playing.