Search Unity

super mario galaxy gravity

Discussion in 'Scripting' started by emannazir, Feb 7, 2012.

  1. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    i want super mario galaxy kind of gravity Character are suposed to move around a small planet.planet may b a sphere a cube cylinder any thing.
     

    Attached Files:

  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    then implement it. where is the issue? what is your question? what does not work in your code? why are you posting?

    i remember that there has been some posts on this topic so search the forum.
     
  3. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    http://forum.unity3d.com/threads/122237-gravity-making-me-mad

    check this i tried on some post about it but it didn't under stand this :( why just in rotation why am not able to implement it in cube? they used Quaternion and freezeRotation i studied these topic from net but i didn't get how to change :s
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    and how should one be able to help when you dont give any usefull information? describe detailed what you have done and what is not working then someone may have an idea. but when you just state "you want" nobody is going to do it for you.
     
  5. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    here it is
     

    Attached Files:

    madsalama likes this.
  6. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    @exiguous check this out
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    if your gravity point is at 5, and you are at 10, then your gravity direction is 5 - 10..... If you move to -10 then your gravity direction is 5 - (-10). Normalize the direction and you get a straight direction of 1 or -1.

    I know... why post something stupid like this....


    World Center is 0,0,0.... My position is 100,100,50. Gravity direction is (World Center - Current Position).normalized.

    Even better... transform.up = -Gravity Direction.normalized

    I don't think that a character controller uses an up vector, so what your asking for may be a created character controller or a physics controlled object. This way, you can specify your up vector and lots of other things.
     
  8. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    how to normalize the directions? where i do this normalization of position? can you specify me in t he code?
     
  9. vdek

    vdek

    Joined:
    Sep 2, 2011
    Posts:
    368
  10. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    did you checked my code?
     
  11. vdek

    vdek

    Joined:
    Sep 2, 2011
    Posts:
    368
    No.

    Off the top of my head, the easiest way to do this is to apply a Spherical Trigger around the planet body, make it the size of the Planetoid + Air. When your player body is within the spherical trigger, Apply a Force Vector towards the center of the planet, psuedocode:
    Code (csharp):
    1.  
    2. float gravityConstant = -5.0f;
    3. Vector3 toCenter = PlayerTransform.position - PlanetoidTransform.position;
    4. toCenter.Normalize();
    5. PlayRigidbody.AddForce(toCenter * gravityConstant);

    Use your triggers to select which Planetoid you are using for your toCenter. This *should* work but might need a bit of tweaking depending on the scale of things and how your players colliders behaves on top of the spherical collider of the planet. You're also going to want to add in code so that your player is standing on his feet when he jumps onto a planet, you could probably do this as soon as you enter the trigger.
     
    Last edited: Feb 7, 2012
  12. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    can you make it in form of code? and hey i made another code of kind of magnate but that is also not working properly you want to see that ? and let me know the problem and help me in correcting them. and can you implement you logic?
     
  13. vdek

    vdek

    Joined:
    Sep 2, 2011
    Posts:
    368
    No I can't, I have my own projects to worry about.
     
  14. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
  15. vdek

    vdek

    Joined:
    Sep 2, 2011
    Posts:
    368
  16. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    While folks here are more than willing to help you out, they are much less likely to write the code for you. You're also not helping your case by posting the same issue in 3 or 4 different threads. In order to get as much help as possible I'd suggest posting code (not in the form or a rar attachment) and asking specific questions about what's not working in your implementation. Then, when you get responses modify your code and if you still have issues repeat the process. Simply posting an attachment with your project and saying "please change my code" isn't going to garner a lot of support.

    If you'd rather have someone just write the code for you - I would suggest posting in the Collaboration or Commercial Work forums.
     
  17. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    here is my another code i tried its working fine but when i just have a prefab to attract but i want some character to attract it look weird :s
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class magScript : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     Collider[] allObjectInField;
    10.  
    11.     public GameObject attractablePref;
    12.  
    13.     GameObject StageCamera;
    14.  
    15.     bool rigidBodyBool;
    16.         public float jumpSpeed = 8.0F;
    17.  
    18.     public float gravity = 20.0F;
    19.  
    20.     private Vector3 moveDirection = Vector3.zero;
    21.  
    22.     public float speed;
    23.  
    24.     CharacterController controller;
    25.     // Use this for initialization
    26.     void Start ()
    27.     {
    28.  
    29.         StageCamera = GameObject.FindGameObjectWithTag("MainCamera");
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update ()
    34.  
    35.     {
    36.  
    37.  
    38.  
    39.  
    40.  
    41.         allObjectInField = Physics.OverlapSphere(transform.position, 20);
    42.  
    43.         if (allObjectInField.Length > 0)
    44.  
    45.         {
    46.             for ( int i = 0; i < allObjectInField.Length;i++)
    47.             {
    48.  
    49.                 if (allObjectInField[i].gameObject.tag == "attractable")
    50.  
    51.                 {
    52.  
    53.  
    54.  
    55.                     Vector3 dir = transform.position - allObjectInField[i].transform.position;
    56.  
    57.                     // for rigid body
    58.  
    59.                    // allObjectInField[i].rigidbody.velocity = dir.normalized * (20.1f - dir.magnitude );
    60.  
    61.  
    62.  
    63.                     // for character Controller
    64.  
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  
    71.          controller = allObjectInField[i].gameObject.GetComponent<CharacterController>();
    72.  
    73.         //if (controller.isGrounded)
    74.  
    75.       //  {
    76.  
    77.             moveDirection = dir;
    78.  
    79.             Debug.DrawLine(transform.position, transform.position + moveDirection, Color.red);
    80.  
    81.             moveDirection = transform.TransformDirection(moveDirection);
    82.  
    83.             moveDirection *= speed;
    84.  
    85.             if (Input.GetButton("Jump"))
    86.  
    87.                 moveDirection.y = jumpSpeed;
    88.  
    89.  
    90.  
    91.         //}
    92.  
    93.         moveDirection.y -= gravity * Time.deltaTime;
    94.  
    95.         controller.Move(moveDirection * Time.deltaTime);
    96.                 }
    97.             }
    98.         }
    99.  
    100.  
    101.  
    102.         if (Input.GetKeyUp(KeyCode.Mouse0))
    103.  
    104.         {
    105.  
    106.             Vector3 newpos = StageCamera.camera.ScreenToWorldPoint(Input.mousePosition);
    107.  
    108.             newpos = new Vector3(newpos.x, 0, newpos.z);
    109.  
    110.             GameObject instantiat = Instantiate(attractablePref, newpos, Quaternion.identity) as GameObject;
    111.  
    112.         }
    113.  
    114.  
    115.     }
    116.  
    117.  
    can any one help me i correcting this?
     
  18. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    First off, this is not going to work with a CharacterController. (Up is always up with a CharacterController)

    Do yourself a favor, start simple. Create an empty scene, make a big ball, call it "Earth" and put a cube above it.

    Give the cube a rigid body, and turn gravity off on it.

    Now, in a Fixed Update, apply this code:
    Code (csharp):
    1.  
    2. Transform PlanetoidTransform = GameObject.Find("Earth").transform;
    3. float gravityConstant = -5.0f;
    4. Vector3 toCenter = transform.position - PlanetoidTransform.position;
    5. toCenter.Normalize();
    6. rigidbody.AddForce(toCenter * gravityConstant);
    7.  
    If you duplicate the cube all over the world, they will start falling towards the planet center.
     
  19. emannazir

    emannazir

    Joined:
    Jan 28, 2012
    Posts:
    138
    its not working :s
     
  20. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    works fine for me....
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class fauxGravity : MonoBehaviour {
    6.     float gravityConstant = -5.0f;
    7.     private Transform PlanetoidTransform;
    8.     // Use this for initialization
    9.     void Start () {
    10.         PlanetoidTransform = GameObject.Find("Earth").transform;
    11.         if(!rigidbody)
    12.             gameObject.AddComponent<Rigidbody>();
    13.         rigidbody.useGravity = false;
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void FixedUpdate () {
    18.         Vector3 toCenter = transform.position - PlanetoidTransform.position;
    19.         toCenter.Normalize();
    20.         rigidbody.AddForce(toCenter * gravityConstant);
    21.     }
    22. }
    23.  
    Even a creator...
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Creator : MonoBehaviour {
    6.    
    7.     public GameObject objToSpawn;
    8.     private float spawnIn=1.0f;
    9.    
    10.     // Update is called once per frame
    11.     void Update () {
    12.         if(!objToSpawn) return;
    13.         spawnIn -= Time.deltaTime;
    14.         if(spawnIn < 0){
    15.             Vector3 pos = Random.insideUnitSphere.normalized;
    16.             pos *= Random.Range(10.0f, 40.0f);
    17.             Instantiate(objToSpawn, pos, Random.rotation);
    18.             spawnIn = Random.Range(1.0f, 3.0f);
    19.         }
    20.     }
    21. }
    22.  
     
  21. ptdnet

    ptdnet

    Joined:
    Apr 20, 2011
    Posts:
    100
    I just threw BigMisterB's code into a scene and it works perfectly.

    Just put different spheres in your universe (level, whatever) and apply gravity based on which sphere you're near. Boom, Mario Galaxy.
     
  22. ajlitynski

    ajlitynski

    Joined:
    Oct 12, 2012
    Posts:
    1
    I'm trying to achieve a similar effect, but with a character controller. You say it wouldn't work with a character controller. Why? Because it's certainly not working with my character controller.

    The CC seems to stand on the sphere, but slowly slides towards the highest Y point on the sphere. If he's below the hemisphere, he'll find equilibrium somewhere and jitter until he shoots through the world to the max Y point where he sits happily.

    Is that the sort of behavior you expected a character controller to exhibit here?
     
  23. Bunzaga

    Bunzaga

    Joined:
    Jan 9, 2009
    Posts:
    202
    For the character controller, 'up' is always World Up, not 'local up'. This works with Rigid Bodies, because you can rotate them around, where the character controller is always up == Vector3(0,1,0).

    Not sure exactly what to expect when you don't use things properly, I would have expected him to just drop off the sphere when he got to the sides.