MegaShapeRBodyPath.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 
  2. using UnityEngine;
  3. public class MegaShapeRBodyPath : MonoBehaviour
  4. {
  5. public MegaShape target; // The Shape that will attract the rigid body
  6. public int curve = 0; // The sub curve of that shape usually 0
  7. public float force = 1.0f; // The force that will applied if the rbody is 1 unit away from the curve
  8. public float alpha = 0.0f; // The alpha value to use is usealpha mode set, allows you to set the point on the curve to attract the rbody (0 - 1)
  9. public bool usealpha = false; // Set to true to use alpha value instead of finding the nearest point on the curve.
  10. Rigidbody rb = null;
  11. void Update()
  12. {
  13. if ( target )
  14. {
  15. target.selcurve = curve;
  16. Vector3 p;
  17. Vector3 pos = transform.position;
  18. if ( usealpha )
  19. p = target.transform.TransformPoint(target.InterpCurve3D(curve, alpha, true));
  20. else
  21. {
  22. Vector3 tangent = Vector3.zero;
  23. int kt = 0;
  24. p = target.FindNearestPointWorld(pos, 5, ref kt, ref tangent, ref alpha);
  25. }
  26. if ( rb == null )
  27. rb = GetComponent<Rigidbody>();
  28. if ( rb )
  29. {
  30. Vector3 dir = p - pos;
  31. rb.AddForce(dir * (force / dir.magnitude));
  32. }
  33. }
  34. }
  35. }