MegaCharacterFollow.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. public class MegaCharacterFollow : MonoBehaviour
  5. {
  6. public MegaShape path;
  7. //public float impulse = 10.0f;
  8. //public float drive = 0.0f;
  9. Rigidbody rbody;
  10. void Start()
  11. {
  12. float alpha = 0.0f;
  13. Vector3 tangent = Vector3.zero;
  14. int kn = 0;
  15. rbody = GetComponent<Rigidbody>();
  16. Vector3 p = transform.position;
  17. Vector3 np = path.FindNearestPointWorld(p, 5, ref kn, ref tangent, ref alpha);
  18. rbody.MovePosition(np);
  19. }
  20. public bool rot = false;
  21. public Vector3 rotate = Vector3.zero;
  22. void LateUpdate()
  23. {
  24. if ( path )
  25. {
  26. Vector3 p = transform.position;
  27. float alpha = 0.0f;
  28. Vector3 tangent = Vector3.zero;
  29. int kn = 0;
  30. Vector3 np = path.FindNearestPointWorld(p, 5, ref kn, ref tangent, ref alpha);
  31. //Vector3 dir = np - p;
  32. if ( rot )
  33. {
  34. Vector3 np1 = path.splines[0].InterpCurve3D(alpha + 0.001f, true, ref kn);
  35. Quaternion er = Quaternion.Euler(rotate);
  36. Quaternion r = Quaternion.LookRotation(np1 - np); //transform.LookAt(target.transform.TransformPoint(target.InterpCurve3D(curve, a + ta, target.normalizedInterp)));
  37. transform.rotation = path.transform.rotation * r * er;
  38. }
  39. np.y = p.y;
  40. transform.position = np;
  41. //rigidbody.AddForce(dir * impulse);
  42. //rigidbody.MovePosition(np);
  43. }
  44. }
  45. }