- Home /
Physics in scene editor mode?
Is there a way to apply gravity and physics to objects within the scene editor without running the game? I have some scenes that can be a bit complex when they start, as object may be slightly out of place until gravity affects them and they go to sleep. This can be a bit taxing when loading levels in iOS, and although only lasts a few seconds, I would prefer if everything is ready to go to sleep from the start.
Alternatively: if i start the game from the editor, I can create a prefab of objects in the position I want, but using this prefab to replace components breaks the original prefab relationship. Is there another way to save these objects positioning without using this scene prefab approach?
can't you just get the final testing position while in the editor, and then just make the objects start at the final resting position ins$$anonymous$$d of waiting for them to settle?
I have been trying that but it gets insanely time consu$$anonymous$$g in large levels with many inclined floors with, sometimes up to a hundreads bodies (reason those levels get so bad at startup)
Answer by Waz · Aug 22, 2011 at 12:20 AM
Yes, just drag the objects from the hierarchy to the project view during runtime. Or copy their transforms (with pen-and-paper or one of the tools you can get for that). You can then start the Rigidbodies Asleep.
Or just hit ctrl+c in play-mode, ctrl+v in edit mode and delete the original.
THAT! THATS what I needed! Darnit, such a simple answer and me here struggling with that!!! I got to admit, I would never had guessed the Unity editor was capable of copying that kind of information in the clipboard.
Thank you!!! I guess I'll flag the answer you replied as the solution since you posted under it :)
Thanks!
Answer by ThePilgrim · Oct 17, 2019 at 08:06 PM
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);
}
}
Answer by Anykey · Aug 21, 2011 at 05:16 PM
I don't really know what you want to do , but I would press Play icon and go back to the scene editor window to see the physic in the editor
Lets say you have 5 boxes. You place them in the map in the scene editor. Once you hit play, they fall to the ground, they may rotate as needed based on the floor angle and any other item that may be in the way.
What I want is to save that positioning so the game starts that way when the level starts up. Reason to do this is that in iOS, the initial process of accommodating these blocks can be very sluggish. In a case of only 5 items, its easy for me to manually edit the xyz position and rotation based off what happened when I hit play, but in much much larger scenes, this becomes extremely hard, tedious and error prone.
honestly, i don't think you really have any other way to do it. off the top of my head there's only two ways that I can see you accomplishing this; 1) as you're building the scene in the first place, use play, get their final position and rotation, and set them as you make the objects, so you don't have to do a pass later to go through them all. 2) write 2 scripts.. one that uses an editor gui button that allows you to save a snapshot of all of the objects position + transform down to a file, ie: keyvalues (or i believe there's a dictionary class on the wiki somewhere, which is the same principle. and then a second script, that when the scene starts, it loads it's "initial position" from the dictionary, from it's unique name.
either way, it's going to be time consu$$anonymous$$g.
Oh, and that also means that in order to do it, you would have to make every object have a unique name, or some unique identifier, so that you can save the keys, and then load them properly.
Yea, the editor scripting sounds interesting and I'll look more into them for my next project.
But I think I'll go back to saving prefabs while within the editor, keeping a duplicate of the entire scene that has the original prefab associations. This was my "fallback" solution from the start but wanted to see if there was some built in feature I may had missed.
Answer by Crazycarpet · May 19 at 06:48 PM
I see this is old but I use this asset: https://assetstore.unity.com/packages/tools/level-design/editor-physics-simulator-221538
Really simple and it lets you record animation clips of the physics simulations.
Your answer
Follow this Question
Related Questions
Ho do I find all scenes with a certain script on an GameObject 2 Answers
Post-Process All Unity Scene GameObjects 1 Answer
Unity 4.6 Copying Complex GameObjects Between Scenes Resets Fields In the Editor 2 Answers
Cannot select multiple items in hierarchy / scene? 6 Answers
Accessing a UnityEditor class. 1 Answer