How can i make the Standard FPS controller to be affected by the gravity of a planet.

Updated on May 9, 2017 in Unity
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
1 on May 9, 2017

I have this script on my planet:

  1. using UnityEngine;
  2. using System.Collections;
  3. public class GravityAttractor : MonoBehaviour {
  4. public float gravity = -9.8f;
  5. public void Attract(Rigidbody body) {
  6. Vector3 gravityUp = (body.position - transform.position).normalized;
  7. Vector3 localUp = body.transform.up;
  8. // Apply downwards gravity to body
  9. body.AddForce(gravityUp * gravity);
  10. // Allign bodies up axis with the centre of planet
  11. body.rotation = Quaternion.FromToRotation(localUp,gravityUp) * body.rotation;
  12. }
  13. }

And this script on the FPS controller:

  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent (typeof (Rigidbody))]
  4. public class GravityBody : MonoBehaviour {
  5. GravityAttractor planet;
  6. Rigidbody rigidbody;
  7. void Awake () {
  8. planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
  9. rigidbody = GetComponent<Rigidbody> ();
  10. // Disable rigidbody gravity and rotation as this is simulated in GravityAttractor script
  11. rigidbody.useGravity = false;
  12. rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
  13. }
  14. void FixedUpdate () {
  15. // Allow this body to be influenced by planet's gravity
  16. planet.Attract(rigidbody);
  17. }
  18. }

But it is not working on the FPS controller.

But it is working on any other gameobject.

Why?????

Please help me.

  • Liked by
Reply
0 on May 9, 2017

Sorry

This code on the planet:

  1. using UnityEngine;
  2. using System.Collections;
  3. public class GravityAttractor : MonoBehaviour {
  4. public float gravity = -9.8f;
  5. public void Attract(Rigidbody body) {
  6. Vector3 gravityUp = (body.position - transform.position).normalized;
  7. Vector3 localUp = body.transform.up;
  8. // Apply downwards gravity to body
  9. body.AddForce(gravityUp * gravity);
  10. // Allign bodies up axis with the centre of planet
  11. body.rotation = Quaternion.FromToRotation(localUp,gravityUp) * body.rotation;
  12. }
  13. }

and this on the FPS controller:

  1. using UnityEngine;
  2. using System.Collections;
  3. [RequireComponent (typeof (Rigidbody))]
  4. public class GravityBody : MonoBehaviour {
  5. GravityAttractor planet;
  6. Rigidbody rigidbody;
  7. void Awake () {
  8. planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
  9. rigidbody = GetComponent<Rigidbody> ();
  10. // Disable rigidbody gravity and rotation as this is simulated in GravityAttractor script
  11. rigidbody.useGravity = false;
  12. rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
  13. }
  14. void FixedUpdate () {
  15. // Allow this body to be influenced by planet's gravity
  16. planet.Attract(rigidbody);
  17. }
  18. }
  • Liked by
Reply
Cancel