Search Unity

Creating Tubular Gravity - A Workable Approach?!

Discussion in 'Physics' started by Shin_Toasty, Oct 1, 2019.

  1. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    46
    I told myself not to post this thread because it's silly... but er, did it anyway.

    Imagine a game inside a tubular spaceship, where "circular" gravity pushes all characters to its interior floor.

    I am trying to do this as simply as possible because the game needs to have hundreds of characters; simple enemies without complex AI though.

    If the ship were a sphere I could create an object in the middle and run AddExplosionForce from it in Update... but it's not.

    Would also be good if the tube could bend, but not essential. I have already disabled normal Unity gravity!
     
  2. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    209
    You mean like Rama, right? https://en.wikipedia.org/wiki/Artificial_gravity

    You could do something like this:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(Rigidbody))]
    4. public class TubularGravity : MonoBehaviour
    5. {
    6.     public Rigidbody rb;
    7.     public Transform center;
    8.     public float gravity = 9.8f; //m/s^2
    9.  
    10.     private void FixedUpdate()
    11.     {
    12.         Vector3 dir = (rb.transform.position - center.position).normalized;
    13.         rb.AddForce(dir*gravity, ForceMode.Acceleration);
    14.     }
    15.  
    16.     private void Reset()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     private void OnDrawGizmosSelected()
    22.     {
    23.         if (center == null || rb == null)
    24.             return;
    25.  
    26.         Gizmos.color = Color.red;
    27.         Gizmos.DrawLine(rb.position, center.position);
    28.     }
    29. }
    30.  
    But I think It won't be enough because you need friction too.

    I mean, with this you can push the bodies against the floor, but you also need that the floor moves the characters in its rotational movement.

    Other solution could be to have the bodies in the ship hierarchy

    artificialgravity.gif
     
    Last edited: Oct 1, 2019
    Shin_Toasty likes this.
  3. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    46
    Just seen your post Maeslezo, thanks a lot. Wasn't sure if anyone would reply. I think your approach leads the only way really - thanks for GIF too. :) Happily, it isn't actually necessary for the ship to literally spin - the game camera will be inside the ship, so could use a revolving sky trick, if needed.

    As long as the game characters are pushed outwards they will collide with my ship's hollow-cylinder-shaped mesh collider and rub along against it.

    With Parenting I could fake the whole gravity system, but it would be too annoying to create, because the objects do enter and exit the ship. I would be parenting and unparenting them too often.

    Ideally, Unity physics would be really sophisticated and I could create a ship like the Discovery in 2001 and its rotation would create the gravity!
     
unityunity