1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    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. if (otherCollider.CompareTag("Player"))
    6. {
    7. target = otherCollider.transform;
    8. Fire();
    9. }
    10. }
    11.  
    12. if (otherCollider.CompareTag("Player"))
    13. {
    14. target = null;
    15. StopCoroutine("Fire"); // aborts the currently running Fire() coroutine
    16. }
    17. }
    18.  
    19. function Fire()
    20. {
    21. while (target != null)
    22. {
    23. var nextFire = Time.time + 2;
    24. while (Time.time < nextFire)
    25. {
    26. transform.LookAt(target);
    27. yield WaitForEndOfFrame();
    28. }
    29.  
    30. // fire!
    31. var bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
    32. bullet.velocity = transform.forward * bulletSpeed;
    33. }
    34. }
    35.  
    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.