Hi I'm trying to make a game similar to Mario Galaxy where you jump from planet to planet. The problem is that my "Home made" gravity dose not allow me to jump. The gravity is dependent on individual objects attracting themself to a specific body. However that makes it so that I can't apply any short upwards forces to it. If someone cold help with that I wold be very grateful, and if it's possible to make it in a separate function that would be helpfull so it's easier to apply the same things to eventual other stuff that has to jump. This is the code for the player movement as of now Code (csharp): public float gravityStength = 9.81f; public float moveSpeed = 10f; public GameObject CurGrav; private Vector3 Gravity; private Transform myTransform; void Start() { //Defining Variables rigidbody.constraints = RigidbodyConstraints.FreezeRotation; rigidbody.useGravity = false; myTransform = transform; } void FixedUpdate() { //Gravity Gravity = (CurGrav.transform.position - myTransform.position).normalized; Vector3 localUp = transform.up; rigidbody.AddForce(Gravity * gravityStength); //Rotating body Quaternion targetRotation = Quaternion.FromToRotation(localUp, Gravity) * myTransform.rotation; myTransform.rotation = Quaternion.Slerp(myTransform.rotation, targetRotation, 3f * Time.deltaTime); //Moving Player transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Time.deltaTime * moveSpeed); transform.Translate(Input.GetAxis("Horizontal") * Vector3.right * Time.deltaTime * moveSpeed); } And here is one with only gravity Code (csharp): public float gravityStength = 5f; public Vector3 Gravity; public GameObject CurGrav; private Transform myTransform; void Start() { rigidbody.constraints = RigidbodyConstraints.FreezeRotation; rigidbody.useGravity = false; myTransform = transform; } void FixedUpdate () { Gravity = (CurGrav.transform.position - myTransform.position).normalized; Vector3 localUp = transform.up; rigidbody.AddForce(Gravity * gravityStength); Quaternion targetRotation = Quaternion.FromToRotation(localUp, Gravity) * myTransform.rotation; myTransform.rotation = Quaternion.Slerp(myTransform.rotation, targetRotation, 3f * Time.deltaTime); } I've attached the script. Please help end excuse my bad english
Here ya go! It uses a simple raycast to detect if the player is on the surface of whatever he's on, and applies a force opposite localUp. I uses a sphere of radius 2.0, so you'll probably have to change the raycast distance to suit your player better. Code (csharp): public float gravityStength = 9.81f; public float moveSpeed = 10f; public GameObject CurGrav; public float jumpForce = 300; private Vector3 Gravity; private Transform myTransform; void Start() { //Defining Variables rigidbody.constraints = RigidbodyConstraints.FreezeRotation; rigidbody.useGravity = false; myTransform = transform; } void FixedUpdate() { //Gravity Gravity = (CurGrav.transform.position - myTransform.position).normalized; Vector3 localUp = transform.up; rigidbody.AddForce(Gravity * gravityStength); //Rotating body Quaternion targetRotation = Quaternion.FromToRotation(localUp, Gravity) * myTransform.rotation; myTransform.rotation = Quaternion.Slerp(myTransform.rotation, targetRotation, 3f * Time.deltaTime); //Moving Player transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Time.deltaTime * moveSpeed); transform.Translate(Input.GetAxis("Horizontal") * Vector3.right * Time.deltaTime * moveSpeed); //you may need to adjust the distance here, it was used for a radius-2 sphere. bool inAir = Physics.Raycast(transform.position, localUp, 1.03f); if(Input.GetButtonDown("Jump") inAir) { print("Bla"); rigidbody.AddForce(-localUp * jumpForce); } } Hope it works for you!