Eonirr

Untitled

Apr 23rd, 2025 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. //Sur le player
  2. public class GravityBody : MonoBehaviour
  3. {
  4. public GravityAttractor objAttractor;
  5. public bool useRbGravity;
  6. private Rigidbody rb;
  7.  
  8. void Awake()
  9. {
  10. rb = GetComponent<Rigidbody>();
  11. rb.useGravity = useRbGravity;
  12. rb.constraints = RigidbodyConstraints.FreezeRotation;
  13. }
  14.  
  15. void FixedUpdate()
  16. {
  17. if (objAttractor!= null) objAttractor.Attract(rb);
  18. }
  19. }
  20.  
  21. //Sur les obj
  22. public class GravityAttractor : MonoBehaviour
  23. {
  24. public float gravity;
  25. public bool invertGravity;
  26.  
  27. public void Attract(Rigidbody rb)
  28. {
  29. Vector3 direction = (transform.position - rb.position).normalized;
  30.  
  31. if (invertGravity) direction = -direction;
  32.  
  33. Vector3 gravityForce = direction * gravity;
  34.  
  35. rb.AddForce(gravityForce, ForceMode.Acceleration);
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment