From ce038d6a718114e0d8304465392141808eadcfc2 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Tue, 8 Oct 2024 10:53:25 +0200 Subject: [PATCH 1/2] Make sure the position of an imported 3d model doesn't overwrite a position set through the loading of a project, or another external method --- .../Scripts/Layers/LayerTypes/ObjSpawner.cs | 21 ++++++++----------- Assets/Scripts/ObjectPlacementUtility.cs | 9 +++++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Assets/Scripts/Layers/LayerTypes/ObjSpawner.cs b/Assets/Scripts/Layers/LayerTypes/ObjSpawner.cs index 6e27d4bb1..4cda29507 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 7c299c1f9..45834ff8b 100644 --- a/Assets/Scripts/ObjectPlacementUtility.cs +++ b/Assets/Scripts/ObjectPlacementUtility.cs @@ -7,14 +7,17 @@ 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 / 2f, Screen.height / 2f)); var plane = new Plane(Vector3.up, 0); var intersect = plane.Raycast(ray, out float distance); if (!intersect) + { distance = 10f; + } - var spawnPoint = camera.transform.position + camera.transform.forward * distance; - return spawnPoint; + return cameraTransform.position + cameraTransform.forward * distance; } } } From 58c78b277265f379840a670e5cc5c55cbf03c4b0 Mon Sep 17 00:00:00 2001 From: Mike van Riel Date: Tue, 22 Oct 2024 07:39:45 +0200 Subject: [PATCH 2/2] Small cleanup as mentioned in PR review --- Assets/Scripts/ObjectPlacementUtility.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/ObjectPlacementUtility.cs b/Assets/Scripts/ObjectPlacementUtility.cs index 45834ff8b..4108a83e0 100644 --- a/Assets/Scripts/ObjectPlacementUtility.cs +++ b/Assets/Scripts/ObjectPlacementUtility.cs @@ -9,10 +9,9 @@ public static Vector3 GetSpawnPoint() var camera = Camera.main; var cameraTransform = camera.transform; - var ray = camera.ScreenPointToRay(new Vector2(Screen.width / 2f, Screen.height / 2f)); + 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; }