Search Unity

gravity

Discussion in 'Scripting' started by markis, Nov 20, 2010.

  1. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
  2. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    Draw the objects towards whatever point is generating gravity instead of drawing them down along the -Y axis.

    That is, have nothing affected by physx gravity (or set it to zero), then all your rigidbodies need a "gravity" script -

    Code (csharp):
    1. function FixedUpdate() {
    2.   var closestGravity : GravityGenerator;
    3.   // find closest gravity generator
    4.   rigidbody.velocity += ( closestGravity.transform.position - transform.position ) * Time.deltaTime * closestGravity.force;
    5. }
     
  3. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
    i don't understand. Can you make example unitypackage? or video how you doing it.
     
  4. EyeSix

    EyeSix

    Joined:
    Sep 7, 2010
    Posts:
    80
    Vicenti is basically telling you to turn PhysX gravity off (it's in your project settings, simply change it to 0 - if you can't find it, look at the script reference and do it in code) and then attach the script he has given you to every object that you want to be under the influence of gravity. Tweak things a little to suit your project (ie, does mass matter?)
     
  5. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
    ok. Now dont understand just one thing. i have men and sphere. Where i need to put what script?
     
  6. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
    then i try to save script i have an error:

    Code (csharp):
    1. Assets/NewBehaviourScript 1.js(5,24): BCE0018: The name 'GravityGenerator' does not denote a valid type ('not found').
     
  7. bateleur

    bateleur

    Joined:
    Feb 18, 2009
    Posts:
    24
    Well yes, that's to be expected, because "GravityGenerator" is the name of the script class for things which draw other objects towards them. So you need to actually create a script called "GravityGenerator" yourself and make sure it has a field called "force" which contains some suitable value (you'll need to experiment) and then attach this script component to the relevant objects.
     
  8. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
    now i'm realy confused... maybe its becouse i'm bad at english and some words i dont understand... But realy anyone can't create example scene? or unitypacage. Please.

    Or video.
     
    Last edited: Nov 20, 2010
  9. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    You need to walk through one or two of Unity's official tutorials, like the Lerpz platformer or the first-person-shooter. Being able to see where that script goes is one of the most basic abilities in Unity. Since it references "rigidbody.velocity", it's going to be altering the velocity of the object you attach it to.

    Create a script named Gravity Generator and paste
    Code (csharp):
    1. var force : float = 1.0;
    2. var forceDistance : float = 100.0;
    3.  
    4. static var all : Array = new Array();
    5.  
    6. function Start() { all.Add( this ); }
    Create a large sphere (we'll call it the planet), make it a GravityGenerator, and set forceDistance to about three times its radius. Make sure it has a collider, though it doesn't need a rigidbody.

    Create a script named FakeGravity and paste
    Code (csharp):
    1. function FixedUpdate() {
    2.   var newVelocity : Vector3 = rigidbody.velocity;
    3.   for ( var GG in GravityGenerator.all ) {
    4.    var difference : Vector3 = GG.transform.position - transform.position;
    5.    if ( difference.magnitude < GG.forceDistance ) {
    6.      var force = (GG.forceDistance - difference.magnitude) / GG.forceDistance * GG.force;
    7.      newVelocity += difference.normalized * force * Time.deltaTime;
    8.    }
    9.   }
    10.   rigidbody.velocity = newVelocity;
    11. }
    Create a second object, move it outside the sphere, and attach FakeGravity to it. Make sure it has a collider and a rigidbody and that it is unaffected by gravity.

    Play with the planet's GravityGenerator settings and see what happens. The object should treat the planet like a planet, accelerating faster as it gets closer. Add a second planet and the object will be attracted to both of them.

    Now your player character just needs to be a FakeGravity rigidbody and they'll be bound by the same rules.
     
    Last edited: Nov 20, 2010
  10. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    I can't say for sure, but I'm not entirely convinced that the effect shown in the linked webplayer is implemented as a gravity effect. It looks more like the standard 'Mario Galaxy' technique to me, where a ray is cast downwards relative to the player, and if an intersection is found, the player's feet (or whatever) are moved to the intersection point and the player itself is (incrementally) aligned with the surface normal at that point.

    IIRC, in the webplayer there are cylindrical objects that the player can walk on. You could probably get some sort of gravity-based method to work using a closest-point test, but just having a bunch of 'point' gravity sources isn't going to give you the right effect, I don't think.

    Again, just speculating, but I'm somewhat doubtful that there's any sort of 'gravity generator' system being used here.
     
  11. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    Ah, that sounds about right. Can't believe I didn't think of that. xD

    I recall there being plenty of specific examples on how to create that kind of system. Try searching the forums for "Mario Galaxy" or "Prey", I think.

    Basically what you'll do is still disable gravity and implement your own -

    Code (csharp):
    1. var localGravity : Vector3;
    2.  
    3. function Update() {
    4.   var rch : RaycastHit;
    5.   if ( Physics.Raycast( transform.position, -transform.up, rch, playerHeight ) ) {
    6.     localGravity = -rch.normal;
    7.   }
    8. }
    Then you use localGravity as the direction for your gravity. It's still quite complex to get something as robust as Mario Galaxy, but those are a few of the basics for you.
     
  12. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    As I am working on such a system, it makes me feel halfway adequate with C# to know that such a feature is robust and complex. xD
    It'd be really interesting to see just how Mario Galaxy's system is programmed. I wonder just how lengthy it is by comparison.