Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Bullet : MonoBehaviour
  4. {
  5. private Transform target;
  6.  
  7. public float speed = 70f;
  8.  
  9. public GameObject impactEffect;
  10.  
  11. public void Seek(Transform _target)
  12. {
  13. target = _target;
  14. }
  15.  
  16. // Update is called once per frame
  17. void Update()
  18. {
  19. if(target == null)
  20. {
  21. Destroy(gameObject);
  22. return;
  23. }
  24.  
  25. Vector3 dir = target.position - transform.position;
  26. float distanceThisFrame = speed * Time.deltaTime;
  27.  
  28. if (dir.magnitude <= distanceThisFrame)
  29. {
  30. HitTarget();
  31. return;
  32.  
  33. }
  34.  
  35. transform.Translate(dir.normalized * distanceThisFrame, Space.World);
  36. transform.LookAt(target);
  37.  
  38. }
  39.  
  40. void HitTarget ()
  41. {
  42. GameObject effectIns = (GameObject)Instantiate(impactEffect, transform.position, transform.rotation);
  43. Destroy(effectIns, 5f);
  44.  
  45. Destroy(target.gameObject);
  46. Destroy(gameObject);
  47.  
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement