I want to slide in an object into view, coming from the right of the screen.
In order to achieve this, I need to position the object next to the camera view, so that it is still not visible, but close enough for a quick slide in.
For example, placing the object at X=1000000 (so that it is surely outside the camera view) for safety wouldn't work as it couldn't slide in quickly enough. For a reasonable slide, I would need to have it really close next to the camera view.
How could I calculate the object's position for that?
Thank you.
Edit:
Here is the script that I'm now using and which I'm still having problems with:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { void Update() { float fDist = Mathf.Abs(Camera.main.transform.position.z - this.transform.position.z); Bounds b = BoundsFromTransform(this.transform); this.transform.position = GetPointLeftOfCamera(Camera.main, fDist, b.size.magnitude / 2); } public static Bounds BoundsFromTransform(Transform uTransform) { Bounds bounds = new Bounds(uTransform.position, Vector3.zero); foreach (Renderer renderer in uTransform.GetComponentsInChildren<Renderer>()) { bounds.Encapsulate(renderer.bounds); } Vector3 nOff = bounds.center - uTransform.position; return new Bounds(nOff, bounds.size); } public static Vector3 GetPointLeftOfCamera(Camera camera, float distance, float goDiameter) { // 1. var ray = camera.ScreenPointToRay(new Vector3(0, camera.pixelHeight / 2f, 0)); // 2. var borderPoint = ray.GetPoint(distance); // 3. var leftPlane = GeometryUtility.CalculateFrustumPlanes(camera)[0]; var frustumLeft = leftPlane.normal; // 4. var halfDiameter = goDiameter / 2f; return borderPoint + frustumLeft * halfDiameter; } public static Vector3 GetPointRightOfCamera(Camera camera, float distance, float goDiameter) { var ray = camera.ScreenPointToRay(new Vector3(camera.pixelWidth - 1, camera.pixelHeight / 2f, 0)); var borderPoint = ray.GetPoint(distance); var rightPlane = GeometryUtility.CalculateFrustumPlanes(camera)[1]; var frustumRight = rightPlane.normal; var halfDiameter = goDiameter / 2f; return borderPoint + frustumRight * halfDiameter; } }