Search Unity

Gravity without a rigidbody script for whoever can use it

Discussion in 'Scripting' started by ushouldhirejb, Nov 25, 2015.

  1. ushouldhirejb

    ushouldhirejb

    Joined:
    May 13, 2014
    Posts:
    36
    Here is line of code used in a flight script on a youtube tutorial to keep the Flying object from passing through the terrain. I changed the > (greater then ) to a < (less then) and in doing so I created a very simple gravity without a rigidbody script.

    Code (JavaScript):
    1. function Update () {
    2.    
    3.      terrainHeightWhereWeAre = Terrain.activeTerrain.SampleHeight( transform.position );
    4.        
    5.         if (terrainHeightWhereWeAre < transform.position.y)
    6.             transform.position = new Vector3(transform.position.x,
    7.                                              terrainHeightWhereWeAre,
    8.                                              transform.position.z);
    9.        
    10.    
    11. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well, that's not "gravity" so much as "lock you onto the ground".