TIANLUN ZHU
TIANLUN ZHU

Reputation: 351

How to let gravity work in editor mode in Unity3D

I am wondering is there any way to let gravity affect items in editor mode? Normally I set the rigidbody to a object and hit play button, then I can see the object will be falling according to gravity. But How can I do this without hitting play button?

I googled a lot and I can't find any document showing me how to do it.

Thanks

Upvotes: 1

Views: 3091

Answers (2)

Codeblockz
Codeblockz

Reputation: 23

I use this asset called 'Editor Physics Simulator' from the Unity asset store.

Link to asset: https://assetstore.unity.com/packages/tools/level-design/editor-physics-simulator-221538

  • It only simulates non-kinematic Rigidbodys.
  • You can simulate all selected Rigidbodys, or ones that are the child of a selected GameObject, or all Rigidbodies at once.
  • You can simulate for a set amount of time (with interrupts) or manually as long as you want, and you can even still undo/redo the simulation with the default Unity editor keybinds.
  • It also lets you record animation clips of the physics simulations.

Upvotes: 0

Ruzihm
Ruzihm

Reputation: 20259

This answer by ThePilgrim on the Unity Q&A site seems to answer this question:

You can simulate physics in the editor by setting Physics.autoSimulation to false, and using Physics.Simulate() to advance physics frame by frame until your objects are settled.

Here is an example editor window:

 using UnityEditor;
 using UnityEngine;

 public class ScenePhysicsTool : EditorWindow {

     private void OnGUI()
     {
         if (GUILayout.Button("Run Physics"))
         {
             StepPhysics();
         }
     }

     private void StepPhysics()
     {
         Physics.autoSimulation = false;
         Physics.Simulate(Time.fixedDeltaTime);
         Physics.autoSimulation = true;
     }

     [MenuItem("Tools/Scene Physics")]
     private static void OpenWindow()
     {
         GetWindow<ScenePhysicsTool>(false, "Physics", true);
     }
 }

Upvotes: 3

Related Questions