Why when adding a Rigidbody to the Spaceships they are falling down ?

using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class CreateSpaceships : MonoBehaviour
{

    public GameObject Spaceship;
    public int spaceshipsStartingHeight = 20;
    [HideInInspector]
    public GameObject[] spaceships;

    // for tracking properties change
    private Vector3 _extents;
    private int _spaceshipCount;
    private float _spaceshipSize;
    private List<int> randomNumbers = new List<int>();

    /// <summary>
    ///     How far to place ships randomly.
    /// </summary>
    public Vector3 Extents;

    /// <summary>
    ///     How many ships wanted.
    /// </summary>
    public int SpaceShipCount;
    public float SpaceShipSize;

    // Use this for initialization
    void Start()
    {

        Clone();
        spaceships = GameObject.FindGameObjectsWithTag("SpaceShip");
    }

    private void OnValidate()
    {
        // prevent wrong values to be entered
        Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
        SpaceShipCount = Mathf.Max(0, SpaceShipCount);
        SpaceShipSize = Mathf.Max(0.0f, SpaceShipSize);
    }

    private void Reset()
    {
        Extents = new Vector3(250.0f, 20.0f, 250.0f);
        SpaceShipCount = 100;
        SpaceShipSize = 20.0f;
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void Clone()
    {
        if (Extents == _extents && SpaceShipCount == _spaceshipCount && Mathf.Approximately(SpaceShipSize, _spaceshipSize))
            return;

        // cleanup
        var ShipsToDestroy = GameObject.FindGameObjectsWithTag("SpaceShip");
        foreach (var t in ShipsToDestroy)
        {
            if (Application.isEditor)
            {
                DestroyImmediate(t);
            }
            else
            {
                Destroy(t);
            }
        }

        var withTag = GameObject.FindWithTag("Terrain");
        if (withTag == null)
            throw new InvalidOperationException("Terrain not found");

        for (var i = 0; i < SpaceShipCount; i++)
        {
            var o = Instantiate(Spaceship);
            o.tag = "SpaceShip";
            o.transform.SetParent(base.gameObject.transform);
            o.transform.localScale = new Vector3(SpaceShipSize, SpaceShipSize, SpaceShipSize);
            o.AddComponent<Rigidbody>();

            // get random position
            var x = Random.Range(-Extents.x, Extents.x);
            var y = Extents.y; // spaceship altitude relative to terrain below
            var z = Random.Range(-Extents.z, Extents.z);

            // now send a ray down terrain to adjust Y according terrain below
            var height = 10000.0f; // should be higher than highest terrain altitude
            var origin = new Vector3(x, height, z);
            var ray = new Ray(origin, Vector3.down);
            RaycastHit hit;
            var maxDistance = 20000.0f;
            var nameToLayer = LayerMask.NameToLayer("Terrain");
            var layerMask = 1 << nameToLayer;
            if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
            {
                var distance = hit.distance;
                y = height - distance + y; // adjust
            }
            else
            {
                Debug.LogWarning("Terrain not hit, using default height !");
            }

            // place !
            o.transform.position = new Vector3(x, y + spaceshipsStartingHeight, z);
        }

        _extents = Extents;
        _spaceshipCount = SpaceShipCount;
        _spaceshipSize = SpaceShipSize;
    }
}

The line:

o.AddComponent<Rigidbody>();

When i’m running the game i see on each Spaceship a rigidbody component and the property Use Gravity is checked true. Using the gravity.

gravity brings rigidbodies down… that’s part of how the physics engine works. That’s normal. I get the feeling you don’t want them to fall down :wink:
Um… you can disable gravity if you don’t want to use it :slight_smile:
Or you can keep them spaceship up in some other way (counteract gravity).

1 Like

Rgidibody.useGravity:

Set it false.

Or if you don’t want ANY forces to impact it. Rigidbody.isKinematic:

Set it true.

1 Like