Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Jumping with "home made" gravity

Discussion in 'Scripting' started by isse26, Jan 20, 2014.

  1. isse26

    isse26

    Joined:
    Dec 8, 2013
    Posts:
    30
    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):
    1.     public float gravityStength = 9.81f;
    2.     public float moveSpeed = 10f;
    3.     public GameObject CurGrav;
    4.  
    5.     private Vector3 Gravity;
    6.     private Transform myTransform;
    7.  
    8.     void Start()
    9.     {
    10.         //Defining Variables
    11.         rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
    12.         rigidbody.useGravity = false;
    13.         myTransform = transform;
    14.     }
    15.  
    16.     void FixedUpdate()
    17.     {
    18.  
    19.         //Gravity
    20.         Gravity = (CurGrav.transform.position - myTransform.position).normalized;
    21.         Vector3 localUp = transform.up;
    22.  
    23.         rigidbody.AddForce(Gravity * gravityStength);
    24.  
    25.         //Rotating body
    26.         Quaternion targetRotation = Quaternion.FromToRotation(localUp, Gravity) * myTransform.rotation;
    27.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, targetRotation, 3f * Time.deltaTime);
    28.  
    29.         //Moving Player
    30.         transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Time.deltaTime * moveSpeed);
    31.         transform.Translate(Input.GetAxis("Horizontal") * Vector3.right * Time.deltaTime * moveSpeed);
    32.     }
    And here is one with only gravity
    Code (csharp):
    1.     public float gravityStength = 5f;
    2.     public Vector3 Gravity;
    3.     public GameObject CurGrav;
    4.  
    5.     private Transform myTransform;
    6.  
    7.     void Start()
    8.     {
    9.         rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
    10.         rigidbody.useGravity = false;
    11.         myTransform = transform;
    12.     }
    13.  
    14.     void FixedUpdate ()
    15.     {
    16.         Gravity = (CurGrav.transform.position - myTransform.position).normalized;
    17.         Vector3 localUp = transform.up;
    18.  
    19.         rigidbody.AddForce(Gravity * gravityStength);
    20.  
    21.         Quaternion targetRotation = Quaternion.FromToRotation(localUp, Gravity) * myTransform.rotation;
    22.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, targetRotation, 3f * Time.deltaTime);
    23.  
    24.  
    25.     }
    I've attached the script.

    Please help end excuse my bad english
     

    Attached Files:

  2. AngryGenius

    AngryGenius

    Joined:
    Jan 21, 2014
    Posts:
    14
    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):
    1.    public float gravityStength = 9.81f;
    2.  
    3.     public float moveSpeed = 10f;
    4.  
    5.     public GameObject CurGrav;
    6.  
    7.     public float jumpForce = 300;
    8.  
    9.     private Vector3 Gravity;
    10.  
    11.     private Transform myTransform;
    12.  
    13.  
    14.  
    15.     void Start()
    16.     {
    17.  
    18.         //Defining Variables
    19.  
    20.         rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
    21.  
    22.         rigidbody.useGravity = false;
    23.  
    24.         myTransform = transform;
    25.  
    26.     }
    27.  
    28.  
    29.  
    30.     void FixedUpdate()
    31.     {
    32.  
    33.  
    34.  
    35.         //Gravity
    36.  
    37.         Gravity = (CurGrav.transform.position - myTransform.position).normalized;
    38.  
    39.         Vector3 localUp = transform.up;
    40.  
    41.  
    42.  
    43.         rigidbody.AddForce(Gravity * gravityStength);
    44.  
    45.  
    46.  
    47.         //Rotating body
    48.  
    49.         Quaternion targetRotation = Quaternion.FromToRotation(localUp, Gravity) * myTransform.rotation;
    50.  
    51.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, targetRotation, 3f * Time.deltaTime);
    52.  
    53.  
    54.  
    55.         //Moving Player
    56.  
    57.         transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Time.deltaTime * moveSpeed);
    58.  
    59.         transform.Translate(Input.GetAxis("Horizontal") * Vector3.right * Time.deltaTime * moveSpeed);
    60.  
    61.         //you may need to adjust the distance here, it was used for a radius-2 sphere.
    62.         bool inAir = Physics.Raycast(transform.position, localUp, 1.03f);
    63.  
    64.         if(Input.GetButtonDown("Jump")  inAir)
    65.         {
    66.             print("Bla");
    67.             rigidbody.AddForce(-localUp * jumpForce);
    68.         }
    69.  
    70.     }
    Hope it works for you! :D