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

Gravity Gun Physx Sample

Discussion in 'Made With Unity' started by friuns3, Nov 11, 2010.

  1. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    Hi
    I made sample project, where i show how to make gravity gun.

    Download

    Features:
    • Full playable fps game sample.
    • Gravity gun script.
    • Cool spark effects.

    Here is video



    Post here your email address. And i send you unitypackage.
     
    Last edited: Dec 29, 2010
  2. lucidcoder

    lucidcoder

    Joined:
    Mar 23, 2010
    Posts:
    138
    This is awesome. How'd you get those sparks working when objects collide?
     
  3. UnleadedGames

    UnleadedGames

    Joined:
    Feb 17, 2008
    Posts:
    242
    Very cool
     
  4. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
  5. defjr

    defjr

    Joined:
    Apr 27, 2009
    Posts:
    436
    Post a unitypackage here. :)
     
  6. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    i wont post it to asset store, but i dont know how =(
     
  7. skledarg

    skledarg

    Joined:
    Nov 7, 2010
    Posts:
    2
  8. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
  9. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
  10. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    why its in iframe at google docs?

    can i submit here free content?
     
  11. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    I believe so. There is free content on there, so you should be able to.
     
  12. raymix

    raymix

    Joined:
    Aug 25, 2010
    Posts:
    192
    awesome idea!
    yes you can post free content there, there's few already.
    Probably they have to test it or something, but reason for delay i heard could be UNITE dev conference
     
  13. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    didn't get yet anything on email...
     
  14. MSylvia

    MSylvia

    Joined:
    May 19, 2009
    Posts:
    42
    It takes a while for them to respond, mine took about a week for a simple reply that I'm now added to the queue.
     
  15. HJP

    HJP

    Joined:
    Jul 8, 2009
    Posts:
    152
    Last edited: Nov 18, 2010
  16. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
    yep, now i received same reply...

    Write your email here, i will send you this sample on email.
     
  17. StephenL

    StephenL

    Joined:
    Oct 18, 2010
    Posts:
    218
  18. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
  19. StephenL

    StephenL

    Joined:
    Oct 18, 2010
    Posts:
    218
    Thanks! :D
     
  20. Samsson

    Samsson

    Joined:
    Oct 12, 2010
    Posts:
    165
    Last edited: Nov 20, 2010
  21. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
    Looks fantastic.
     
  22. friuns3

    friuns3

    Joined:
    Oct 30, 2009
    Posts:
    307
  23. Esoasnipe

    Esoasnipe

    Joined:
    Feb 2, 2011
    Posts:
    1
  24. QuantumCalzone

    QuantumCalzone

    Joined:
    Jan 9, 2010
    Posts:
    262
  25. alex.loren

    alex.loren

    Joined:
    Jan 7, 2011
    Posts:
    83
    If I could receive a copy of the package that would be outstanding! Email address is AlexLoren@gmail.com , Thanks in advance!
     
  26. thelastowl

    thelastowl

    Joined:
    Jul 30, 2010
    Posts:
    71
    unity3mall requires a minimum spend even though your package is free it still will cost on there email me please instead (lastowl @ lastowl.info) (no spaces)
     
  27. 2dfxman1

    2dfxman1

    Joined:
    Oct 6, 2010
    Posts:
    1,065
  28. Rush-Rage-Games

    Rush-Rage-Games

    Joined:
    Sep 9, 2010
    Posts:
    1,997
    How did you get the objects to move, I added the cube script, changed the mass and the physical material to box and added a ridged body(I'm playing in the example).

    Thanks!
     
  29. DigitalAdam

    DigitalAdam

    Joined:
    Jul 18, 2007
    Posts:
    1,202
    I'd love to get a copy too! :)

    adamz(at)blacksunfx(dot)com
     
  30. impheris

    impheris

    Joined:
    Dec 30, 2009
    Posts:
    1,623
  31. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
  32. dtntr

    dtntr

    Joined:
    Oct 6, 2009
    Posts:
    26
    Cool!
     
  33. Krawler

    Krawler

    Joined:
    Jul 18, 2011
    Posts:
    1
  34. ggappleid

    ggappleid

    Joined:
    Nov 2, 2010
    Posts:
    82
    Asome asome asome Please send here ggappleid@gmail.com
     
  35. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,887
  36. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    If anyone is interested, the way you do it is very easy.

    1. make a gameobject called attract as a child of the player ahead of the player (preferbly the camera), where you want stuff to go to.

    2. make a sphere collider trigger for that game object, make it as large as the radius ought to be.

    3. onTriggerEnter script for each object.

    The script simply use addforce to push the object towards the trigger, it honestly couldn't be simpler. The direction to use addforce with is a Vector3 subtracted from the trigger position. There's other ways too, its very easy indeed.

    I post this in case people asking for this script, cannot obtain it.
     
  37. rustam0241

    rustam0241

    Joined:
    Aug 7, 2011
    Posts:
    3
  38. pixelsteam

    pixelsteam

    Joined:
    May 1, 2009
    Posts:
    924
  39. omgitsalexl

    omgitsalexl

    Joined:
    Jun 20, 2011
    Posts:
    70
    For those of you still requesting the package, here is the code to a working gravity gun based off of this package. The code is pretty horrid and was never made for production use; but feel free to use it as a starting point / reference for your own gravity gun.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class GravityGun : MonoBehaviour
    5. {
    6.     /*
    7.      * Class members
    8.      */
    9.     public float m_Force = 80.0f;
    10.     public float m_Gravity = 1.0f;
    11.     public float m_Radius = 10.0f;
    12.     public float m_ScaleFactor = 10.0f;
    13.     public Transform m_RayTarget;
    14.     public Transform m_RayLight;
    15.     public float m_RaySpeed = 1.0f;
    16.     public float m_RayScale = 1.0f;
    17.     public int m_RayZigs = 100;
    18.     private Perlin m_Noise;
    19.     private Particle[] m_Particles;
    20.     private float m_RayZigsAdded;
    21.     private GameObject m_TempObject;
    22.    
    23.    
    24.     /*
    25.      * Private start class method
    26.      */
    27.     private void Start()
    28.     {
    29.         // Set particle member fields
    30.         this.m_RayZigsAdded = 1.0f / (float)this.m_RayZigs;
    31.         this.particleEmitter.Emit();
    32.         this.m_Particles = this.particleEmitter.particles;
    33.        
    34.         // Disable ray light
    35.         this.m_RayLight.light.enabled = false;
    36.     }
    37.    
    38.    
    39.     /*
    40.      * Private update class method
    41.      */
    42.     public void Update()
    43.     {
    44.         if(Input.GetMouseButtonUp(0) == true)
    45.         {
    46.             // Iterate through all gravity objects
    47.             foreach(GameObject go in GameObject.FindGameObjectsWithTag("Gravity Object"))
    48.             {
    49.                 // Set temporary variable
    50.                 this.m_TempObject = go;
    51.                
    52.                 // Calculate distance to game object
    53.                 if(Vector3.Distance(go.transform.position, this.transform.position) < this.m_Radius / 4.5f)
    54.                 {
    55.                     // Apply force to game object
    56.                     go.rigidbody.angularDrag = 2.0f;
    57.                     go.rigidbody.AddForce(this.transform.rotation * new Vector3(0, 0, this.m_Force * this.m_ScaleFactor));
    58.                 }
    59.             }
    60.            
    61.             // Disable ray light
    62.             this.m_RayLight.light.enabled = false;
    63.         }
    64.        
    65.         // Check if temporary game object is set
    66.         if(this.m_TempObject != null)
    67.         {
    68.             // Calculate distance to game object
    69.             if(Vector3.Distance(this.m_TempObject.transform.position, this.transform.position) > this.m_Radius)
    70.             {
    71.                 // Disable ray light
    72.                 this.m_RayLight.light.enabled = false;
    73.             }
    74.         }
    75. }
    76.    
    77.    
    78.     /*
    79.      * Private fixed update class method
    80.      */
    81.     public void FixedUpdate()
    82.     {
    83.         // Check if left mouse button is down
    84.         if(Input.GetMouseButton(0) == true)
    85.         {
    86.             // Iterate through all gravity objects
    87.             foreach(GameObject go in GameObject.FindGameObjectsWithTag("Gravity Object"))
    88.             {
    89.                 go.rigidbody.AddExplosionForce(-this.m_Gravity * this.m_ScaleFactor, GameObject.Find("Atractor").transform.position, this.m_Radius);
    90.                 go.rigidbody.angularDrag = 30.0f;
    91.                 go.rigidbody.velocity *= 0.97f;
    92.                
    93.                 // Calculate distance to game object
    94.                 if(Vector3.Distance(go.transform.position, this.transform.position) < this.m_Radius)
    95.                 {
    96.                     // Create ray particles
    97.                     this.CreateRay();
    98.                    
    99.                     // Enable ray light
    100.                     this.m_RayLight.light.enabled = true;
    101.                 }
    102.             }
    103.         }
    104.     }
    105.    
    106.    
    107.     /*
    108.      * Private create ray class method
    109.      */
    110.     private void CreateRay()
    111.     {
    112.         if(this.m_Noise == null)
    113.             this.m_Noise = new Perlin();
    114.            
    115.         float timex = Time.time * this.m_RaySpeed * 0.1365143f;
    116.         float timey = Time.time * this.m_RaySpeed * 1.21688f;
    117.         float timez = Time.time * this.m_RaySpeed * 2.5564f;
    118.        
    119.         for (int i=0; i < this.m_Particles.Length; i++)
    120.         {
    121.             Vector3 StartingOffset = new Vector3(this.transform.position.x + 0.3f, this.transform.position.y - 0.3f, this.transform.position.z);
    122.             Vector3 StartingPosition = Vector3.Lerp(StartingOffset, this.m_RayTarget.position, this.m_RayZigsAdded * (float)i);
    123.             Vector3 OffsetPosition = new Vector3(this.m_Noise.Noise(timex + StartingPosition.x, timex + StartingPosition.y, timex + StartingPosition.z),
    124.                                                 this.m_Noise.Noise(timey + StartingPosition.x, timey + StartingPosition.y, timey + StartingPosition.z),
    125.                                                 this.m_Noise.Noise(timez + StartingPosition.x, timez + StartingPosition.y, timez + StartingPosition.z));
    126.             StartingPosition += (OffsetPosition * this.m_RayScale * ((float)i * this.m_RayZigsAdded));
    127.            
    128.             this.m_Particles[i].position = StartingPosition;
    129.             this.m_Particles[i].color = Color.white;
    130.             this.m_Particles[i].energy = 1f;
    131.         }
    132.        
    133.         particleEmitter.particles = this.m_Particles;
    134.     }
    135. }
    136.  
     
  40. rustam0241

    rustam0241

    Joined:
    Aug 7, 2011
    Posts:
    3
    omgitsalexl

    Assets/GravityGun.cs(17,21): error CS0246: The type or namespace name `Perlin' could not be found. Are you missing a using directive or an assembly reference?
     
  41. omgitsalexl

    omgitsalexl

    Joined:
    Jun 20, 2011
    Posts:
    70
    The perlin methods came from the Unity Procedural Examples, and you can get them there or simply copy and paste the following into it's own source file. But like I stated in the above post, the gravity gun script was never made for production use and should be rewritten if any real use is intended.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System;
    4. using UnityEngine;
    5.  
    6. /* Perlin noise use example:
    7.  
    8. Perlin perlin = new Perlin();
    9. var value : float = perlin.Noise(2);
    10. var value : float = perlin.Noise(2, 3, );
    11. var value : float = perlin.Noise(2, 3, 4);
    12.  
    13.  
    14. SmoothRandom use example:
    15.  
    16. var p = SmoothRandom.GetVector3(3);
    17.  
    18. */
    19.  
    20. public class SmoothRandom
    21. {
    22.     public static Vector3 GetVector3 (float speed)
    23.     {
    24.         float time = Time.time * 0.01F * speed;
    25.         return new Vector3(Get().HybridMultifractal(time, 15.73F, 0.58F), Get().HybridMultifractal(time, 63.94F, 0.58F), Get().HybridMultifractal(time, 0.2F, 0.58F));
    26.     }
    27.    
    28.     public static float Get (float speed)
    29.     {
    30.         float time = Time.time * 0.01F * speed;
    31.         return Get().HybridMultifractal(time * 0.01F, 15.7F, 0.65F);
    32.     }
    33.  
    34.     private static FractalNoise Get () {
    35.         if (s_Noise == null)
    36.             s_Noise = new FractalNoise (1.27F, 2.04F, 8.36F);
    37.         return s_Noise;    
    38.      }
    39.  
    40.     private static FractalNoise s_Noise;
    41. }
    42.  
    43.  
    44. public class Perlin
    45. {
    46.     // Original C code derived from
    47.     // http://astronomy.swin.edu.au/~pbourke/texture/perlin/perlin.c
    48.     // http://astronomy.swin.edu.au/~pbourke/texture/perlin/perlin.h
    49.     const int B = 0x100;
    50.     const int BM = 0xff;
    51.     const int N = 0x1000;
    52.  
    53.     int[] p = new int[B + B + 2];
    54.     float[,] g3 = new float [B + B + 2 , 3];
    55.     float[,] g2 = new float[B + B + 2,2];
    56.     float[] g1 = new float[B + B + 2];
    57.  
    58.     float s_curve(float t)
    59.     {
    60.         return t * t * (3.0F - 2.0F * t);
    61.     }
    62.    
    63.     float lerp (float t, float a, float b)
    64.     {
    65.         return a + t * (b - a);
    66.     }
    67.  
    68.     void setup (float value, out int b0, out int b1, out float r0, out float r1)
    69.     {
    70.         float t = value + N;
    71.         b0 = ((int)t)  BM;
    72.         b1 = (b0+1)  BM;
    73.         r0 = t - (int)t;
    74.         r1 = r0 - 1.0F;
    75.     }
    76.    
    77.     float at2(float rx, float ry, float x, float y) { return rx * x + ry * y; }
    78.     float at3(float rx, float ry, float rz, float x, float y, float z) { return rx * x + ry * y + rz * z; }
    79.  
    80.     public float Noise(float arg)
    81.     {
    82.         int bx0, bx1;
    83.         float rx0, rx1, sx, u, v;
    84.         setup(arg, out bx0, out bx1, out rx0, out rx1);
    85.        
    86.         sx = s_curve(rx0);
    87.         u = rx0 * g1[ p[ bx0 ] ];
    88.         v = rx1 * g1[ p[ bx1 ] ];
    89.        
    90.         return(lerp(sx, u, v));
    91.     }
    92.  
    93.     public float Noise(float x, float y)
    94.     {
    95.         int bx0, bx1, by0, by1, b00, b10, b01, b11;
    96.         float rx0, rx1, ry0, ry1, sx, sy, a, b, u, v;
    97.         int i, j;
    98.        
    99.         setup(x, out bx0, out bx1, out rx0, out rx1);
    100.         setup(y, out by0, out by1, out ry0, out ry1);
    101.        
    102.         i = p[ bx0 ];
    103.         j = p[ bx1 ];
    104.        
    105.         b00 = p[ i + by0 ];
    106.         b10 = p[ j + by0 ];
    107.         b01 = p[ i + by1 ];
    108.         b11 = p[ j + by1 ];
    109.        
    110.         sx = s_curve(rx0);
    111.         sy = s_curve(ry0);
    112.        
    113.         u = at2(rx0,ry0, g2[ b00, 0 ], g2[ b00, 1 ]);
    114.         v = at2(rx1,ry0, g2[ b10, 0 ], g2[ b10, 1 ]);
    115.         a = lerp(sx, u, v);
    116.        
    117.         u = at2(rx0,ry1, g2[ b01, 0 ], g2[ b01, 1 ]);
    118.         v = at2(rx1,ry1, g2[ b11, 0 ], g2[ b11, 1 ]);
    119.         b = lerp(sx, u, v);
    120.        
    121.         return lerp(sy, a, b);
    122.     }
    123.    
    124.     public float Noise(float x, float y, float z)
    125.     {
    126.         int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
    127.         float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v;
    128.         int i, j;
    129.        
    130.         setup(x, out bx0, out bx1, out rx0, out rx1);
    131.         setup(y, out by0, out by1, out ry0, out ry1);
    132.         setup(z, out bz0, out bz1, out rz0, out rz1);
    133.        
    134.         i = p[ bx0 ];
    135.         j = p[ bx1 ];
    136.        
    137.         b00 = p[ i + by0 ];
    138.         b10 = p[ j + by0 ];
    139.         b01 = p[ i + by1 ];
    140.         b11 = p[ j + by1 ];
    141.        
    142.         t  = s_curve(rx0);
    143.         sy = s_curve(ry0);
    144.         sz = s_curve(rz0);
    145.        
    146.         u = at3(rx0,ry0,rz0, g3[ b00 + bz0, 0 ], g3[ b00 + bz0, 1 ], g3[ b00 + bz0, 2 ]);
    147.         v = at3(rx1,ry0,rz0, g3[ b10 + bz0, 0 ], g3[ b10 + bz0, 1 ], g3[ b10 + bz0, 2 ]);
    148.         a = lerp(t, u, v);
    149.        
    150.         u = at3(rx0,ry1,rz0, g3[ b01 + bz0, 0 ], g3[ b01 + bz0, 1 ], g3[ b01 + bz0, 2 ]);
    151.         v = at3(rx1,ry1,rz0, g3[ b11 + bz0, 0 ], g3[ b11 + bz0, 1 ], g3[ b11 + bz0, 2 ]);
    152.         b = lerp(t, u, v);
    153.        
    154.         c = lerp(sy, a, b);
    155.        
    156.         u = at3(rx0,ry0,rz1, g3[ b00 + bz1, 0 ], g3[ b00 + bz1, 2 ], g3[ b00 + bz1, 2 ]);
    157.         v = at3(rx1,ry0,rz1, g3[ b10 + bz1, 0 ], g3[ b10 + bz1, 1 ], g3[ b10 + bz1, 2 ]);
    158.         a = lerp(t, u, v);
    159.        
    160.         u = at3(rx0,ry1,rz1, g3[ b01 + bz1, 0 ], g3[ b01 + bz1, 1 ], g3[ b01 + bz1, 2 ]);
    161.         v = at3(rx1,ry1,rz1,g3[ b11 + bz1, 0 ], g3[ b11 + bz1, 1 ], g3[ b11 + bz1, 2 ]);
    162.         b = lerp(t, u, v);
    163.        
    164.         d = lerp(sy, a, b);
    165.        
    166.         return lerp(sz, c, d);
    167.     }
    168.    
    169.     void normalize2(ref float x, ref float y)
    170.     {
    171.        float s;
    172.    
    173.         s = (float)Math.Sqrt(x * x + y * y);
    174.         x = y / s;
    175.         y = y / s;
    176.     }
    177.    
    178.     void normalize3(ref float x, ref float y, ref float z)
    179.     {
    180.         float s;
    181.         s = (float)Math.Sqrt(x * x + y * y + z * z);
    182.         x = y / s;
    183.         y = y / s;
    184.         z = z / s;
    185.     }
    186.    
    187.     public Perlin()
    188.     {
    189.         int i, j, k;
    190.         System.Random rnd = new System.Random();
    191.    
    192.        for (i = 0 ; i < B ; i++) {
    193.           p[i] = i;
    194.           g1[i] = (float)(rnd.Next(B + B) - B) / B;
    195.    
    196.           for (j = 0 ; j < 2 ; j++)
    197.              g2[i,j] = (float)(rnd.Next(B + B) - B) / B;
    198.           normalize2(ref g2[i, 0], ref g2[i, 1]);
    199.    
    200.           for (j = 0 ; j < 3 ; j++)
    201.              g3[i,j] = (float)(rnd.Next(B + B) - B) / B;
    202.              
    203.    
    204.           normalize3(ref g3[i, 0], ref g3[i, 1], ref g3[i, 2]);
    205.        }
    206.    
    207.        while (--i != 0) {
    208.           k = p[i];
    209.           p[i] = p[j = rnd.Next(B)];
    210.           p[j] = k;
    211.        }
    212.    
    213.        for (i = 0 ; i < B + 2 ; i++) {
    214.           p[B + i] = p[i];
    215.           g1[B + i] = g1[i];
    216.           for (j = 0 ; j < 2 ; j++)
    217.              g2[B + i,j] = g2[i,j];
    218.           for (j = 0 ; j < 3 ; j++)
    219.              g3[B + i,j] = g3[i,j];
    220.        }
    221.     }
    222. }
    223.  
    224. public class FractalNoise
    225. {
    226.     public FractalNoise (float inH, float inLacunarity, float inOctaves)
    227.         : this (inH, inLacunarity, inOctaves, null)
    228.     {
    229.        
    230.     }
    231.  
    232.     public FractalNoise (float inH, float inLacunarity, float inOctaves, Perlin noise)
    233.     {
    234.         m_Lacunarity = inLacunarity;
    235.         m_Octaves = inOctaves;
    236.         m_IntOctaves = (int)inOctaves;
    237.         m_Exponent = new float[m_IntOctaves+1];
    238.         float frequency = 1.0F;
    239.         for (int i = 0; i < m_IntOctaves+1; i++)
    240.         {
    241.             m_Exponent[i] = (float)Math.Pow (m_Lacunarity, -inH);
    242.             frequency *= m_Lacunarity;
    243.         }
    244.        
    245.         if (noise == null)
    246.             m_Noise = new Perlin();
    247.         else
    248.             m_Noise = noise;
    249.     }
    250.    
    251.    
    252.     public float HybridMultifractal(float x, float y, float offset)
    253.     {
    254.         float weight, signal, remainder, result;
    255.        
    256.         result = (m_Noise.Noise (x, y)+offset) * m_Exponent[0];
    257.         weight = result;
    258.         x *= m_Lacunarity;
    259.         y *= m_Lacunarity;
    260.         int i;
    261.         for (i=1;i<m_IntOctaves;i++)
    262.         {
    263.             if (weight > 1.0F) weight = 1.0F;
    264.             signal = (m_Noise.Noise (x, y) + offset) * m_Exponent[i];
    265.             result += weight * signal;
    266.             weight *= signal;
    267.             x *= m_Lacunarity;
    268.             y *= m_Lacunarity;
    269.         }
    270.         remainder = m_Octaves - m_IntOctaves;
    271.         result += remainder * m_Noise.Noise (x,y) * m_Exponent[i];
    272.        
    273.         return result;
    274.     }
    275.    
    276.     public float RidgedMultifractal (float x, float y, float offset, float gain)
    277.     {
    278.         float weight, signal, result;
    279.         int i;
    280.        
    281.         signal = Mathf.Abs (m_Noise.Noise (x, y));
    282.         signal = offset - signal;
    283.         signal *= signal;
    284.         result = signal;
    285.         weight = 1.0F;
    286.    
    287.         for (i=1;i<m_IntOctaves;i++)
    288.         {
    289.             x *= m_Lacunarity;
    290.             y *= m_Lacunarity;
    291.            
    292.             weight = signal * gain;
    293.             weight = Mathf.Clamp01 (weight);
    294.            
    295.             signal = Mathf.Abs (m_Noise.Noise (x, y));
    296.             signal = offset - signal;
    297.             signal *= signal;
    298.             signal *= weight;
    299.             result += signal * m_Exponent[i];
    300.         }
    301.    
    302.         return result;
    303.     }
    304.  
    305.     public float BrownianMotion (float x, float y)
    306.     {
    307.         float value, remainder;
    308.         long i;
    309.        
    310.         value = 0.0F;
    311.         for (i=0;i<m_IntOctaves;i++)
    312.         {
    313.             value = m_Noise.Noise (x,y) * m_Exponent[i];
    314.             x *= m_Lacunarity;
    315.             y *= m_Lacunarity;
    316.         }
    317.         remainder = m_Octaves - m_IntOctaves;
    318.         value += remainder * m_Noise.Noise (x,y) * m_Exponent[i];
    319.        
    320.         return value;
    321.     }
    322.  
    323.    
    324.     private Perlin  m_Noise;
    325.     private float[] m_Exponent;
    326.     private int     m_IntOctaves;
    327.     private float   m_Octaves;
    328.     private float   m_Lacunarity;
    329. }
    330. /*
    331.  
    332. /// This is an alternative implementation of perlin noise
    333. public class Noise
    334. {
    335.     public float Noise(float x)
    336.     {
    337.         return Noise(x, 0.5F);
    338.     }
    339.  
    340.     public float Noise(float x, float y)
    341.     {
    342.         int Xint = (int)x;
    343.         int Yint = (int)y;
    344.         float Xfrac = x - Xint;
    345.         float Yfrac = y - Yint;
    346.  
    347.         float x0y0 = Smooth_Noise(Xint, Yint);  //find the noise values of the four corners
    348.         float x1y0 = Smooth_Noise(Xint+1, Yint);
    349.         float x0y1 = Smooth_Noise(Xint, Yint+1);
    350.         float x1y1 = Smooth_Noise(Xint+1, Yint+1);
    351.  
    352.         //interpolate between those values according to the x and y fractions
    353.         float v1 = Interpolate(x0y0, x1y0, Xfrac); //interpolate in x direction (y)
    354.         float v2 = Interpolate(x0y1, x1y1, Xfrac); //interpolate in x direction (y+1)
    355.         float fin = Interpolate(v1, v2, Yfrac);  //interpolate in y direction
    356.  
    357.         return fin;
    358.     }
    359.  
    360.     private float Interpolate(float x, float y, float a)
    361.     {
    362.         float b = 1-a;
    363.         float fac1 = (float)(3*b*b - 2*b*b*b);
    364.         float fac2 = (float)(3*a*a - 2*a*a*a);
    365.  
    366.         return x*fac1 + y*fac2; //add the weighted factors
    367.     }
    368.  
    369.     private float GetRandomValue(int x, int y)
    370.     {
    371.         x = (x+m_nNoiseWidth) % m_nNoiseWidth;
    372.         y = (y+m_nNoiseHeight) % m_nNoiseHeight;
    373.         float fVal = (float)m_aNoise[(int)(m_fScaleX*x), (int)(m_fScaleY*y)];
    374.         return fVal/255*2-1f;
    375.     }
    376.  
    377.     private float Smooth_Noise(int x, int y)
    378.     {
    379.         float corners = ( Noise2d(x-1, y-1) + Noise2d(x+1, y-1) + Noise2d(x-1, y+1) + Noise2d(x+1, y+1) ) / 16.0f;
    380.         float sides = ( Noise2d(x-1, y) +Noise2d(x+1, y) + Noise2d(x, y-1) + Noise2d(x, y+1) ) / 8.0f;
    381.         float center = Noise2d(x, y) / 4.0f;
    382.         return corners + sides + center;
    383.     }
    384.  
    385.     private float Noise2d(int x, int y)
    386.     {
    387.         x = (x+m_nNoiseWidth) % m_nNoiseWidth;
    388.         y = (y+m_nNoiseHeight) % m_nNoiseHeight;
    389.                
    390.         float fVal = (float)m_aNoise[(int)(m_fScaleX*x), (int)(m_fScaleY*y)];
    391.        
    392.         return fVal/255*2-1f;
    393.     }
    394.  
    395.     public Noise()
    396.     {
    397.         m_nNoiseWidth = 100;
    398.         m_nNoiseHeight = 100;
    399.         m_fScaleX = 1.0F;
    400.         m_fScaleY = 1.0F;
    401.         System.Random rnd = new System.Random();
    402.         m_aNoise = new int[m_nNoiseWidth,m_nNoiseHeight];
    403.         for (int x = 0; x<m_nNoiseWidth; x++)
    404.         {
    405.             for (int y = 0; y<m_nNoiseHeight; y++)
    406.             {
    407.                 m_aNoise[x,y] = rnd.Next(255);
    408.             }
    409.         }
    410.     }
    411.    
    412.     private int[,] m_aNoise;
    413.     protected int m_nNoiseWidth, m_nNoiseHeight;
    414.     private float m_fScaleX, m_fScaleY;
    415. }
    416.  
    417.  
    418. /*  Yet another perlin noise implementation. This one is not even completely ported to C#
    419.  
    420.    
    421.     float noise1[];
    422.     float noise2[];
    423.     float noise3[];
    424.     int indices[];
    425.  
    426.     float PerlinSmoothStep (float t)
    427.     {
    428.         return t * t * (3.0f - 2.0f * t);
    429.     }
    430.    
    431.     float PerlinLerp(float t, float a, float b)
    432.     {
    433.         return a + t * (b - a);
    434.     }
    435.    
    436.     float PerlinRand()
    437.     {
    438.         return Random.rand () / float(RAND_MAX)  * 2.0f - 1.0f;
    439.     }
    440.    
    441.    
    442.     PerlinNoise::PerlinNoise ()
    443.     {
    444.         long i, j, k;
    445.         float x, y, z, denom;
    446.        
    447.         Random rnd = new Random();
    448.  
    449.                
    450.         noise1 = new float[1 * (PERLIN_B + PERLIN_B + 2)];
    451.         noise2 = new float[2 * (PERLIN_B + PERLIN_B + 2)];
    452.         noise3 = new float[3 * (PERLIN_B + PERLIN_B + 2)];
    453.         indices = new long[PERLIN_B + PERLIN_B + 2];
    454.        
    455.         for (i = 0; i < PERLIN_B; i++)
    456.         {
    457.             indices[i] = i;
    458.    
    459.             x = PerlinRand();
    460.             y = PerlinRand();
    461.             z = PerlinRand();
    462.    
    463.             noise1[i] = x;
    464.    
    465.             denom = sqrt(x * x + y * y);
    466.             if (denom > 0.0001f) denom = 1.0f / denom;
    467.    
    468.             j = i << 1;
    469.             noise2[j + 0] = x * denom;
    470.             noise2[j + 1] = y * denom;
    471.    
    472.             denom = sqrt(x * x + y * y + z * z);
    473.             if (denom > 0.0001f) denom = 1.0f / denom;
    474.    
    475.             j += i;
    476.             noise3[j + 0] = x * denom;
    477.             noise3[j + 1] = y * denom;
    478.             noise3[j + 2] = z * denom;
    479.         }
    480.    
    481.         while (--i != 0)
    482.         {
    483.             j = rand()  PERLIN_BITMASK;
    484.             std::swap (indices[i], indices[j]);
    485.         }
    486.    
    487.         for (i = 0; i < PERLIN_B + 2; i++)
    488.         {
    489.             j = i + PERLIN_B;
    490.    
    491.             indices[j] = indices[i];
    492.    
    493.             noise1[j] = noise1[i];
    494.    
    495.             j = j << 1;
    496.             k = i << 1;
    497.             noise2[j + 0] = noise2[k + 0];
    498.             noise2[j + 1] = noise2[k + 1];
    499.    
    500.             j += i + PERLIN_B;
    501.             k += i + PERLIN_B;
    502.             noise3[j + 0] = noise3[k + 0];
    503.             noise3[j + 1] = noise3[k + 1];
    504.             noise3[j + 2] = noise3[k + 2];
    505.         }
    506.     }
    507.    
    508.     PerlinNoise::~PerlinNoise ()
    509.     {
    510.         delete []noise1;
    511.         delete []noise2;
    512.         delete []noise3;
    513.         delete []indices;
    514.     }
    515.    
    516.     void PerlinSetup (float v, long b0, long b1, float r0, float r1);
    517.     void PerlinSetup(
    518.         float v,
    519.         long b0,
    520.         long b1,
    521.         float r0,
    522.         float r1)
    523.     {
    524.         v += PERLIN_N;
    525.    
    526.         long vInt = (long)v;
    527.    
    528.         b0 = vInt  PERLIN_BITMASK;
    529.         b1 = (b0 + 1)  PERLIN_BITMASK;
    530.         r0 = v - (float)vInt;
    531.         r1 = r0 - 1.0f;
    532.     }
    533.    
    534.    
    535.     float PerlinNoise::Noise1 (float x)
    536.     {
    537.         long bx0, bx1;
    538.         float rx0, rx1, sx, u, v;
    539.    
    540.         PerlinSetup(x, bx0, bx1, rx0, rx1);
    541.    
    542.         sx = PerlinSmoothStep(rx0);
    543.    
    544.         u = rx0 * noise1[indices[bx0]];
    545.         v = rx1 * noise1[indices[bx1]];
    546.    
    547.         return PerlinLerp (sx, u, v);
    548.     }
    549.    
    550.     float PerlinNoise::Noise2(float x, float y)
    551.     {
    552.         long bx0, bx1, by0, by1, b00, b01, b10, b11;
    553.         float rx0, rx1, ry0, ry1, sx, sy, u, v, a, b;
    554.    
    555.         PerlinSetup (x, bx0, bx1, rx0, rx1);
    556.         PerlinSetup (y, by0, by1, ry0, ry1);
    557.    
    558.         sx = PerlinSmoothStep (rx0);
    559.         sy = PerlinSmoothStep (ry0);
    560.    
    561.         b00 = indices[indices[bx0] + by0] << 1;
    562.         b10 = indices[indices[bx1] + by0] << 1;
    563.         b01 = indices[indices[bx0] + by1] << 1;
    564.         b11 = indices[indices[bx1] + by1] << 1;
    565.    
    566.         u = rx0 * noise2[b00 + 0] + ry0 * noise2[b00 + 1];
    567.         v = rx1 * noise2[b10 + 0] + ry0 * noise2[b10 + 1];
    568.         a = PerlinLerp (sx, u, v);
    569.    
    570.         u = rx0 * noise2[b01 + 0] + ry1 * noise2[b01 + 1];
    571.         v = rx1 * noise2[b11 + 0] + ry1 * noise2[b11 + 1];
    572.         b = PerlinLerp (sx, u, v);
    573.    
    574.         u = PerlinLerp (sy, a, b);
    575.        
    576.         return u;
    577.     }
    578.    
    579.     float PerlinNoise::Noise3(float x, float y, float z)
    580.     {
    581.         long bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
    582.         float rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
    583.    
    584.         PerlinSetup (x, bx0, bx1, rx0, rx1);
    585.         PerlinSetup (y, by0, by1, ry0, ry1);
    586.         PerlinSetup (z, bz0, bz1, rz0, rz1);
    587.    
    588.         b00 = indices[indices[bx0] + by0] << 1;
    589.         b10 = indices[indices[bx1] + by0] << 1;
    590.         b01 = indices[indices[bx0] + by1] << 1;
    591.         b11 = indices[indices[bx1] + by1] << 1;
    592.      
    593.         t = PerlinSmoothStep (rx0);
    594.         sy = PerlinSmoothStep (ry0);
    595.         sz = PerlinSmoothStep (rz0);
    596.        
    597.         #define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
    598.    
    599.         q = &noise3[b00 + bz0]; u = at3(rx0,ry0,rz0);
    600.         q = &noise3[b10 + bz0]; v = at3(rx1,ry0,rz0);
    601.         a = PerlinLerp(t, u, v);
    602.    
    603.         q = &noise3[b01 + bz0]; u = at3(rx0,ry1,rz0);
    604.         q = &noise3[b11 + bz0]; v = at3(rx1,ry1,rz0);
    605.         b = PerlinLerp(t, u, v);
    606.    
    607.         c = PerlinLerp(sy, a, b);
    608.    
    609.         q = &noise3[b00 + bz1]; u = at3(rx0,ry0,rz1);
    610.         q = &noise3[b10 + bz1]; v = at3(rx1,ry0,rz1);
    611.         a = PerlinLerp(t, u, v);
    612.    
    613.         q = &noise3[b01 + bz1]; u = at3(rx0,ry1,rz1);
    614.         q = &noise3[b11 + bz1]; v = at3(rx1,ry1,rz1);
    615.         b = PerlinLerp(t, u, v);
    616.    
    617.         d = PerlinLerp(sy, a, b);
    618.    
    619.         return PerlinLerp (sz, c, d);
    620.     }
    621. */
    622.  
     
    Last edited: Aug 8, 2011
  42. JasonB

    JasonB

    Joined:
    Dec 29, 2010
    Posts:
    102
    Looks cool. My only suggestion would be to increase gravity, because it was quite floaty (not your fault though, just an odd thing with PhysX's gravity not seeming accurate even when the proportions are perfectly accurate).
     
  43. JakeMorris0173

    JakeMorris0173

    Joined:
    Nov 8, 2010
    Posts:
    35
  44. rustam0241

    rustam0241

    Joined:
    Aug 7, 2011
    Posts:
    3
    omgitsalexl
    Explain how to put on, I did not understand or somebody throw off unitypackage
     
  45. ilanbps

    ilanbps

    Joined:
    Mar 22, 2011
    Posts:
    3
  46. omgitsalexl

    omgitsalexl

    Joined:
    Jun 20, 2011
    Posts:
    70
    rustam0241, to use the script you simply attach it, like you would any other component, to the game object that you want to be used as your gravity gun and fill out the needed fields within the inspector.
     
  47. ShadoX

    ShadoX

    Joined:
    Aug 25, 2010
    Posts:
    260
    Reminds me a lot of this game/tech demo.. :)

    Would you mind sending me the package? I'd love to see some of the code.. D:

    I sent you a PM with my mail. (don't want to post it here)
     
  48. Unamine

    Unamine

    Joined:
    Mar 23, 2011
    Posts:
    28
  49. RoDester

    RoDester

    Joined:
    May 21, 2011
    Posts:
    64
  50. uvavoo

    uvavoo

    Joined:
    Oct 15, 2007
    Posts:
    60
    "rustam0241, to use the script you simply attach it, like you would any other component, to the game object that you want to be used as your gravity gun and fill out the needed fields within the inspector."

    Hi thanks for this script. Just trying to figure it out. What goes in the 'Ray Target and Ray Light' areas of the inspector. Also noticed in the script a reference to GameObject 'Atractor'??

    As I am just using the script as supplied earlier and don't have the example file a quick guide on how to set this up would be useful. Thanks.