Skip to content

Commit

Permalink
Make sure the position of an imported 3d model doesn't overwrite a po…
Browse files Browse the repository at this point in the history
…sition set through the loading of a project, or another external method
  • Loading branch information
mvriel committed Oct 21, 2024
1 parent 6b99ffd commit ce038d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 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);
}
}
}
9 changes: 6 additions & 3 deletions Assets/Scripts/ObjectPlacementUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit ce038d6

Please sign in to comment.