/// <summary> /// Attract/Repell the gameobjects of the container which are within the brush /// </summary> /// <param name="hit"></param> /// <param name="attract"></param> private void Magnet(RaycastHit hit, bool attract) { // just some arbitrary value depending on the magnet strength which ranges from 0..100 float magnetFactor = editorTarget.interactionSettings.magnetStrength / 1000f; Transform[] containerChildren = PrefabUtils.GetContainerChildren(editorTarget.container); foreach (Transform transform in containerChildren) { Vector3 distance = hit.point - transform.position; // only those within the brush if (distance.magnitude > editorTarget.brushSettings.brushSize / 2f) { continue; } Vector3 direction = distance.normalized; transform.position += direction * magnetFactor * (attract ? 1 : -1); } }
/// <summary> /// Increment y-position in world space /// </summary> /// <param name="hit"></param> private void AntiGravity(RaycastHit hit) { // just some arbitrary value depending on the magnet strength which ranges from 0..100 float antiGravityFactor = editorTarget.interactionSettings.antiGravityStrength / 1000f; Transform[] containerChildren = PrefabUtils.GetContainerChildren(editorTarget.container); foreach (Transform transform in containerChildren) { Vector3 distance = hit.point - transform.position; // only those within the brush if (distance.magnitude > editorTarget.brushSettings.brushSize / 2f) { continue; } // https://docs.unity3d.com/ScriptReference/Transform-up.html // https://docs.unity3d.com/ScriptReference/Vector3-up.html transform.position += Vector3.up * antiGravityFactor; } }
private void ApplyPhysics() { PhysicsSimulation physicsSimulation = ScriptableObject.CreateInstance <PhysicsSimulation>(); PhysicsSettings physicsSettings = new PhysicsSettings(); physicsSettings.simulationStepCountMax = gizmo.brushSettings.autoSimulationStepCountMax; physicsSettings.simulationStepIterations = gizmo.brushSettings.autoSimulationStepIterations; physicsSimulation.ApplySettings(physicsSettings); // TODO: use only the new added ones? Transform[] containerChildren = PrefabUtils.GetContainerChildren(gizmo.container); if (gizmo.brushSettings.autoSimulationType == BrushSettings.AutoSimulationType.Once) { physicsSimulation.RunSimulationOnce(containerChildren); } else if (gizmo.brushSettings.autoSimulationType == BrushSettings.AutoSimulationType.Continuous) { physicsSimulation.StartSimulation(containerChildren); } }
// TODO: create common class private Transform[] getContainerChildren() { return(PrefabUtils.GetContainerChildren(gizmo.container)); }
private void StartSimulation() { Transform[] containerChildren = PrefabUtils.GetContainerChildren(editorTarget.container); AutoPhysicsSimulation.ApplyPhysics(editorTarget.physicsSettings, containerChildren, SpawnSettings.AutoSimulationType.Continuous); }
public static void ApplyPhysics(PhysicsSettings physicsSettings, GameObject container, SpawnSettings.AutoSimulationType autoSimulationType) { Transform[] containerChildren = PrefabUtils.GetContainerChildren(container); ApplyPhysics(physicsSettings, containerChildren, autoSimulationType); }