Search Unity

My first sucessful script! (Planet Gravity...)

Discussion in 'Scripting' started by cheatsguy777, Jan 23, 2010.

  1. cheatsguy777

    cheatsguy777

    Joined:
    Nov 23, 2009
    Posts:
    29
    I just recently found unity and have been trying to use it to create planet gravity (like you can find in Super Mario Galaxy)and I have finally suceeded. the script is below (NOTE: must have a rigidbody inside the object):
    Code (csharp):
    1.  
    2. var planet : GameObject;
    3. var gravity = 2;
    4. function Update(){
    5. rigidbody.velocity.x = ((-transform.position.x + planet.transform.position.x)*gravity);
    6. rigidbody.velocity.y = ((-transform.position.y + planet.transform.position.y)*gravity);
    7. rigidbody.velocity.z = ((-transform.position.z + planet.transform.position.z)*gravity);
    8. transform.LookAt(planet.transform.position);
    9. }


    sorry, I had to access each vector individually to make it work. change the value gravity to determine the power of the gravity. also, define the planet by changing the planet gameobject.[/code]
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    All of your physics/rigidbody code should be under FixedUpdate() instead of Update().
     
  3. cheatsguy777

    cheatsguy777

    Joined:
    Nov 23, 2009
    Posts:
    29
    ok. I'll fix that.
     
  4. cheatsguy777

    cheatsguy777

    Joined:
    Nov 23, 2009
    Posts:
    29
    ok. Here's the new code:
    Code (csharp):
    1.  
    2. var planet : GameObject;
    3. var gravity = 2;
    4. function FixedUpdate(){
    5. rigidbody.velocity.x = ((-transform.position.x + planet.transform.position.x)*gravity);
    6. rigidbody.velocity.y = ((-transform.position.y + planet.transform.position.y)*gravity);
    7. rigidbody.velocity.z = ((-transform.position.z + planet.transform.position.z)*gravity);
    8. transform.LookAt(planet.transform.position);
    9. }
    10.  
    anyone have a comment on it?
     
  5. cheatsguy777

    cheatsguy777

    Joined:
    Nov 23, 2009
    Posts:
    29
    any comments? suggestions? Ideas?
     
  6. Wox

    Wox

    Joined:
    Dec 14, 2009
    Posts:
    93
    isn't this the same as your x, y, z ?


    rigidbody.velocity = (-transform.position + planet.transform.position) * gravity ;

    transform.LookAt(planet.transform.position);
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's Javascript.

    --Eric
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I did a cut&paste; it compiles fine with no errors.

    --Eric
     
  9. cheatsguy777

    cheatsguy777

    Joined:
    Nov 23, 2009
    Posts:
    29
    Ok. A couple of things just to clear it up... It's in JavaScript, it needs to have an object assigned to it, and it needs to have a non-kinematic(is that spelled right?) rigidbody that doesn't use gravity to function correctly. Also, I tried the assigning the whole velocity but it wouldn't work unless I assigned them individually.
     
  10. cheatsguy777

    cheatsguy777

    Joined:
    Nov 23, 2009
    Posts:
    29
    Oh, if the first person script applies gravity itself this won't work at all.
     
  11. Darloc

    Darloc

    Joined:
    Feb 16, 2011
    Posts:
    167
    Wox I tried it and it does work well with that segment, as far as I can tell the exact same
     
  12. Darloc

    Darloc

    Joined:
    Feb 16, 2011
    Posts:
    167
    I have changed this to be based on distance from the object is there a better way to do it?


    var planet : GameObject;
    var gravity = 2;
    function FixedUpdate(){
    rigidbody.velocity = ((-transform.position + planet.transform.position) * gravity)/ (Mathf.Abs(-transform.position.x + planet.transform.position.x) + Mathf.Abs(-transform.position.y + planet.transform.position.y) + Mathf.Abs(-transform.position.z + planet.transform.position.z)) ;
    transform.LookAt(planet.transform.position);
    }
     
  13. Wiebren-de-Haan

    Wiebren-de-Haan

    Joined:
    Nov 1, 2012
    Posts:
    4
    Hello , i have one problem, i want to make the gravity 0.2 but i cannot put a point in the variable? how do i fix this!
     
  14. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You have to explicitily specify the type of variable. You want a float, but without specifying it the compiler thinks you mean an int.

    So change this:

    var gravity = 2;

    to this:

    var gravity : float = 2.0;
     
  15. Kevinominal

    Kevinominal

    Joined:
    Mar 23, 2013
    Posts:
    1
    Exactly how do I set this up? I made a sphere with a third person controller prefab to test it. I put this script on the sphere, added a rigid body with no gravity and non kinematic and its not working. I dont understand what to do now.
     
  16. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    If you are using the chracter controller component, a rigidbody will not have any effect, you can't reliably combine the two. The posted script is for rigidbodies only
     
  17. chucky831

    chucky831

    Joined:
    Aug 24, 2012
    Posts:
    265
    I did not understand what you want to do anyway to make it less fast-falling you could work on the drag rigidbody
     
  18. welcome druid

    welcome druid

    Joined:
    Apr 4, 2013
    Posts:
    1
    how do you assing it ?
     
  19. malikcgcs

    malikcgcs

    Joined:
    Jun 11, 2013
    Posts:
    9
  20. lance12367

    lance12367

    Joined:
    Dec 30, 2015
    Posts:
    2
    thanks guys this helped alot