diff --git a/Assets/Scripts/Layers/LayerTypes/ObjSpawner.cs b/Assets/Scripts/Layers/LayerTypes/ObjSpawner.cs index 6e27d4bb..4cda2950 100644 --- a/Assets/Scripts/Layers/LayerTypes/ObjSpawner.cs +++ b/Assets/Scripts/Layers/LayerTypes/ObjSpawner.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -20,6 +21,11 @@ public class ObjSpawner : MonoBehaviour, ILayerWithPropertyData private ObjImporter.ObjImporter importer; + private void Awake() + { + gameObject.transform.position = ObjectPlacementUtility.GetSpawnPoint(); + } + private void Start() { StartImport(); @@ -68,8 +74,10 @@ private void ImportObj(string path) private void OnObjImported(GameObject returnedGameObject) { + // By explicitly stating the worldPositionStays to false, we ensure Obj is spawned and it will retain the + // position and scale in this parent object returnedGameObject.transform.SetParent(this.transform, false); - AddLayerScriptToObj(returnedGameObject); + returnedGameObject.AddComponent(); DisposeImporter(); } @@ -78,16 +86,5 @@ private void DisposeImporter() { if (importer != null) Destroy(importer.gameObject); } - - private void AddLayerScriptToObj(GameObject parsedObj) - { - var spawnPoint = ObjectPlacementUtility.GetSpawnPoint(); - - gameObject.transform.position = spawnPoint; - - parsedObj.AddComponent(); - - // CreatedMoveableGameObject.Invoke(parsedObj); - } } } \ No newline at end of file diff --git a/Assets/Scripts/ObjectPlacementUtility.cs b/Assets/Scripts/ObjectPlacementUtility.cs index 7c299c1f..4108a83e 100644 --- a/Assets/Scripts/ObjectPlacementUtility.cs +++ b/Assets/Scripts/ObjectPlacementUtility.cs @@ -7,14 +7,16 @@ public static class ObjectPlacementUtility public static Vector3 GetSpawnPoint() { var camera = Camera.main; - var ray = camera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2)); + var cameraTransform = camera.transform; + + var ray = camera.ScreenPointToRay(new Vector2(Screen.width * .5f, Screen.height * .5f)); var plane = new Plane(Vector3.up, 0); - var intersect = plane.Raycast(ray, out float distance); - if (!intersect) + if (plane.Raycast(ray, out var distance) == false) + { distance = 10f; + } - var spawnPoint = camera.transform.position + camera.transform.forward * distance; - return spawnPoint; + return cameraTransform.position + cameraTransform.forward * distance; } } }