Spirits_Float.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Spirits_Float : MonoBehaviour
  5. {
  6. private bool isRise = true;
  7. public float maxHeight, minHeight;
  8. private float height; //Éýµ½Äĸö¸ß¶ÈºóÍ£Ö¹
  9. private float curHeight;
  10. private float speed = 1;
  11. private Vector3 origPos;
  12. public GameObject effect;
  13. private void Start()
  14. {
  15. origPos = transform.position;
  16. curHeight = origPos.y;
  17. height = Random.Range(minHeight, maxHeight);
  18. Demonic dem = GetComponent<Demonic>();
  19. dem.flyHeight = height;
  20. Instantiate(effect, transform.position, new Quaternion(0, 0, 0, 0), transform);
  21. }
  22. private void MoveToHeight()
  23. {
  24. curHeight = Mathf.SmoothDamp(curHeight, height, ref speed, 0.02f);
  25. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  26. }
  27. private void Update()
  28. {
  29. if (isRise)
  30. {
  31. MoveToHeight();
  32. if (curHeight - height <= 0.02f && curHeight - height >= -0.02f)
  33. {
  34. isRise = false;
  35. curHeight = height;
  36. transform.position = new Vector3(origPos.x, height, origPos.z);
  37. this.enabled = false;
  38. }
  39. }
  40. }
  41. }