暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using UnityEngine;
  2. public class BeeScript : MonoBehaviour
  3. {
  4. private Vector3 Min = new Vector3(-20, 0, -20);
  5. private Vector3 Max = new Vector3(20, 20, 20);
  6. Vector3 target;
  7. Vector3 direction;
  8. Rigidbody rb;
  9. Collider[] bees;
  10. public float speed = 1;
  11. void Start()
  12. {
  13. target = GetNewPosition();
  14. rb = GetComponent<Rigidbody>();
  15. }
  16. void FixedUpdate()
  17. {
  18. direction = target - transform.position;
  19. if (direction.magnitude < 5)
  20. {
  21. target = GetNewPosition();
  22. }
  23. rb.AddForce(direction * speed, ForceMode.Acceleration);
  24. bees = Physics.OverlapSphere(transform.position, 10, 1 << 9);
  25. for (int i = 1; i < bees.Length; i++)
  26. {
  27. rb.AddForce((transform.position - bees[i].transform.position) * 10);
  28. }
  29. }
  30. Vector3 GetNewPosition()
  31. {
  32. float x, y, z;
  33. x = Random.Range(Min.x, Max.x);
  34. y = Random.Range(Min.y, Max.y);
  35. z = Random.Range(Min.z, Max.z);
  36. return new Vector3(x, y, z);
  37. }
  38. }