Add dynamic calculation of the canvas spawn point

This commit is contained in:
Santiago Lo Coco 2024-11-09 23:53:50 +01:00
parent 27dedec6ca
commit c7f644ca55
1 changed files with 9 additions and 6 deletions

View File

@ -66,16 +66,17 @@ public class EndpointLoader : MonoBehaviour
private Vector3 CalculateNextPosition() private Vector3 CalculateNextPosition()
{ {
Transform cameraTransform = Camera.main.transform;
Vector3 localOffset = new Vector3(-0.4f, 0.1f, 1f);
if (instantiatedItems.Count == 0) if (instantiatedItems.Count == 0)
{ {
return new Vector3(-0.4f, 0.1f, 1); return cameraTransform.position + cameraTransform.TransformDirection(localOffset);
} }
GameObject lastItem = instantiatedItems[instantiatedItems.Count - 1]; GameObject lastItem = instantiatedItems[instantiatedItems.Count - 1];
localOffset = new Vector3(localOffset.x + GetItemWidth(lastItem), localOffset.y, localOffset.z);
Vector3 lastPosition = lastItem.transform.position; return cameraTransform.position + cameraTransform.TransformDirection(localOffset);
float itemWidth = GetItemWidth(lastItem);
return new Vector3(lastPosition.x + itemWidth, lastPosition.y, lastPosition.z);
} }
private float GetItemWidth(GameObject item) private float GetItemWidth(GameObject item)
@ -94,11 +95,13 @@ public class EndpointLoader : MonoBehaviour
if (dynamicItem != null) if (dynamicItem != null)
{ {
Vector3 nextPosition = CalculateNextPosition(); Vector3 nextPosition = CalculateNextPosition();
Transform cameraTransform = Camera.main.transform;
Quaternion rotation = Quaternion.LookRotation(cameraTransform.forward, cameraTransform.up);
GameObject newItem = Instantiate( GameObject newItem = Instantiate(
dynamicItem, dynamicItem,
nextPosition, nextPosition,
dynamicItem.transform.rotation, rotation,
dynamicItem.transform.parent dynamicItem.transform.parent
); );
newItem.SetActive(true); newItem.SetActive(true);