Skip to content

Commit

Permalink
Merge pull request #298 from Netherlands3D/fix/ensure-obj-is-at-same-…
Browse files Browse the repository at this point in the history
…location-after-save

Make sure the position of an imported 3d model doesn't overwrite a po…
  • Loading branch information
mvriel authored Oct 22, 2024
2 parents b3ebe85 + 58c78b2 commit 9a871f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
21 changes: 9 additions & 12 deletions Assets/Scripts/Layers/LayerTypes/ObjSpawner.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -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();
Expand Down Expand Up @@ -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<MeshCollider>();

DisposeImporter();
}
Expand All @@ -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<MeshCollider>();

// CreatedMoveableGameObject.Invoke(parsedObj);
}
}
}
12 changes: 7 additions & 5 deletions Assets/Scripts/ObjectPlacementUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit 9a871f3

Please sign in to comment.