Collectable_Script.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Collectable_Script : MonoBehaviour
  5. {
  6. public int scoreValue = 100;
  7. public int healthValue = 0;
  8. public int lifeValue = 0;
  9. public bool floating = true;
  10. private float startY;
  11. void Start()
  12. {
  13. startY = gameObject.transform.position.y;
  14. }
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. Rigidbody2D rigidbody = gameObject.GetComponent<Rigidbody2D>();
  19. if (gameObject.transform.position.y > startY + 10)
  20. {
  21. Destroy(gameObject);
  22. }
  23. if (Vector2.Distance(gameObject.transform.position, GameObject.FindWithTag("Player").transform.position) < 15)
  24. {
  25. if (floating)
  26. {
  27. if (gameObject.transform.position.y < startY + 0.333)
  28. {
  29. gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * Random.Range(0.1f, 1.0f));
  30. }
  31. else if (gameObject.transform.position.y > startY - 0.333)
  32. {
  33. gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.down * Random.Range(0.1f, 1.0f));
  34. }
  35. }
  36. }
  37. else if (gameObject.GetComponent<Collider2D>().enabled)
  38. {
  39. gameObject.transform.position = new Vector2(gameObject.transform.position.x, startY);
  40. }
  41. }
  42. }