Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Creating local gravity areas in Unity3D

Discussion in 'Scripting' started by LuckyWonton, Aug 18, 2019.

  1. LuckyWonton

    LuckyWonton

    Joined:
    Feb 28, 2014
    Posts:
    19
    I'm trying to create dynamic gravity zones in Unity3D. This is different from simulating gravitational attraction.

    Imagine a 10x10x10 cube on a platform in space. The body of this is a trigger, or zone, that simulates Earth's force of -9.81. Immediately off the platform, gravity does not exist. If something were thrown from this platform, it would receive relative force until it fell off, and then continue on forever. If someone slipped off this platform, they would be stranded.

    If I use Rigidbodies and toggle the UseGravity bool on leaving/entering triggers, nothing seems to happen. As far as I can see, UseGravity does nothing. This would by far be the easiest solution though: simply apply universal gravity, but disable gravity on Rigidbodies leaving the Gravity Zone.

    If I set Unity's gravity force to 0,0,0 then gravity is off, great, but I don't, see an efficient way to continue simulating gravity. I have attempted to add a ConstantForce component with 0,-9.81,0 Force, but that's not doing anything.

    Any thoughts?
     
  2. LuckyWonton

    LuckyWonton

    Joined:
    Feb 28, 2014
    Posts:
    19
    I guess to answer my own question:

    Rigidbody.UseGravity does indeed have a valid purpose and it can be toggled off to act as if in space.

    However, the CharacterController script will inhibit gravity and it must be applied manually. The dummy script in this works great.
    https://docs.unity3d.com/ScriptReference/CharacterController.Move.html

    You also have to use CharacterController.Move, not CharacterController.SimpleMove.

    Reposting it here.
    Code (CSharp):
    1.     [RequireComponent(typeof(CharacterController))]
    2.     public class PlayerController : MonoBehaviour {
    3.         CharacterController characterController;
    4.  
    5.         public float speed = 6.0f;
    6.         public float jumpSpeed = 8.0f;
    7.         public float gravity = 20.0f;
    8.  
    9.         private Vector3 moveDirection = Vector3.zero;
    10.  
    11.         void Start() {
    12.             characterController = GetComponent<CharacterController>();
    13.         }
    14.  
    15.         void Update() {
    16.             if (characterController.isGrounded) {
    17.                 // We are grounded, so recalculate
    18.                 // move direction directly from axes
    19.  
    20.                 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
    21.                 moveDirection *= speed;
    22.  
    23.                 if (Input.GetButton("Jump")) {
    24.                     moveDirection.y = jumpSpeed;
    25.                 }
    26.             }
    27.  
    28.             // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
    29.             // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
    30.             // as an acceleration (ms^-2)
    31.             moveDirection.y -= gravity * Time.deltaTime;
    32.  
    33.             // Move the controller
    34.             characterController.Move(moveDirection * Time.deltaTime);
    35.         }
    36.     }