From c7f644ca552083ae7977bddb2e70db177414d7f2 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sat, 9 Nov 2024 23:53:50 +0100 Subject: [PATCH] Add dynamic calculation of the canvas spawn point --- Assets/Scripts/EndpointLoader.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/EndpointLoader.cs b/Assets/Scripts/EndpointLoader.cs index 4ae17d4..c61ee64 100644 --- a/Assets/Scripts/EndpointLoader.cs +++ b/Assets/Scripts/EndpointLoader.cs @@ -66,16 +66,17 @@ public class EndpointLoader : MonoBehaviour private Vector3 CalculateNextPosition() { + Transform cameraTransform = Camera.main.transform; + Vector3 localOffset = new Vector3(-0.4f, 0.1f, 1f); + if (instantiatedItems.Count == 0) { - return new Vector3(-0.4f, 0.1f, 1); + return cameraTransform.position + cameraTransform.TransformDirection(localOffset); } GameObject lastItem = instantiatedItems[instantiatedItems.Count - 1]; - - Vector3 lastPosition = lastItem.transform.position; - float itemWidth = GetItemWidth(lastItem); - return new Vector3(lastPosition.x + itemWidth, lastPosition.y, lastPosition.z); + localOffset = new Vector3(localOffset.x + GetItemWidth(lastItem), localOffset.y, localOffset.z); + return cameraTransform.position + cameraTransform.TransformDirection(localOffset); } private float GetItemWidth(GameObject item) @@ -94,11 +95,13 @@ public class EndpointLoader : MonoBehaviour if (dynamicItem != null) { Vector3 nextPosition = CalculateNextPosition(); + Transform cameraTransform = Camera.main.transform; + Quaternion rotation = Quaternion.LookRotation(cameraTransform.forward, cameraTransform.up); GameObject newItem = Instantiate( dynamicItem, nextPosition, - dynamicItem.transform.rotation, + rotation, dynamicItem.transform.parent ); newItem.SetActive(true);