Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Sur le player
- public class GravityBody : MonoBehaviour
- {
- public GravityAttractor objAttractor;
- public bool useRbGravity;
- private Rigidbody rb;
- void Awake()
- {
- rb = GetComponent<Rigidbody>();
- rb.useGravity = useRbGravity;
- rb.constraints = RigidbodyConstraints.FreezeRotation;
- }
- void FixedUpdate()
- {
- if (objAttractor!= null) objAttractor.Attract(rb);
- }
- }
- //Sur les obj
- public class GravityAttractor : MonoBehaviour
- {
- public float gravity;
- public bool invertGravity;
- public void Attract(Rigidbody rb)
- {
- Vector3 direction = (transform.position - rb.position).normalized;
- if (invertGravity) direction = -direction;
- Vector3 gravityForce = direction * gravity;
- rb.AddForce(gravityForce, ForceMode.Acceleration);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment