Sprite Overlay Question

Hi, I am trying to get a sprite over my gameobject positioned how it would look with no sprite. I managed to get it to work with a orthogonal camera.

With a perspective camera, I don’t know how far to make the z axis before I transform it. 2.6 seems to work but I’d like to know the math.

a is the rect I used to capture a screenshot of the object spritego.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(a.position.x, Screen.height - a.position.y, 2.6f));

So my question is why does 2.6 work as the z? Thanks.

I’m having a hard time understanding what the line even means? You have a gameobject with no sprite, and you have a second GO with a sprite your trying to position in the world closer to the camera “in front” of it?

Anyway not sure what your trying to do, but here’s how ScreenToWorldPoint works and understanding that might help with your problem. In WorldSpace we have 3 dimensions out there. x, y (generally left/right and up/down in relation to the camera) and z a distance away from the camera. With all sorts of maths we can squish all these points in space to a single 2D plane (your screen) that only has x,y points. Now of this means there is a whole line of points that squish down into a single point on the screen. The graphics engine figures out which one of those would have been in front and only displays that one.

So going backwards, if we are taking a single x,y point and putting it back into the world, we have an infinite line to choose from. You supply the Z -Axis co-ordinate of how far away from the camera that point should be stuck at. In perspective world if your twice as big as me, but i’m twice as close as you… i’ll look exactly the same size. So some combination of that is why 2.6 is working for you. Maybe your objects are the same size, so the object your covering is 2.6 units from the camera. Or its 50% bigger and sitting at 3.9… etc.

Building on @takatok 's answer, what you could do is Vector3.Distance between the target game object’s position and the camera’s position.

Its not the distance, it works no matter how far it is from camera. Here’s my code. Line 82 is where 2.6 is. It takes a screenshot on a second camera on layer 8:

using UnityEngine;
using System.Collections;

public class getobjectss : MonoBehaviour
{
    public GameObject target;
    private RenderTexture rt;
    public Camera cam;

    void Start()
    {
        rt = new RenderTexture(Screen.width,Screen.height,24,RenderTextureFormat.ARGB32,RenderTextureReadWrite.Linear);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            StartCoroutine(thisone());
        }
    }

    public IEnumerator thisone()
    {
        yield return new WaitForEndOfFrame();
        target.layer = 8;

        Rect a = GUIRectWithObject(target);
        a = new Rect((int)a.position.x-5, (int)a.position.y-5, (int)a.width+10, (int)a.height+10);
        if (a.xMin < 0)
        {
            a.xMin = 0;
        }
        if (a.xMax > Screen.width)
        {
            a.xMax = Screen.width;
        }
        if (a.yMin < 0)
        {
            a.yMin = 0;
        }
        if (a.yMax > Screen.height)
        {
            a.yMax = Screen.height;
        }
        //Debug.Log(a.xMin + ", "+a.xMax+", "+ a.yMin + ", "+ a.yMax);
        //Debug.Log(a.width + ", " + a.height);
        //Debug.Log(a.position);
        cam.targetTexture = rt;
        RenderTexture.active = rt;
        cam.Render();
        Texture2D screenShot = new Texture2D((int)a.width, (int)a.height, TextureFormat.ARGB32, false, true);
        screenShot.ReadPixels(a, 0, 0, false);
        //byte[] bytes = screenShot.EncodeToPNG();
        //string filename = Application.dataPath + "/_SCap/tredg.png";
        //System.IO.File.WriteAllBytes(filename, bytes);
        RenderTexture.active = null;
        cam.targetTexture = null;
        target.layer = 0;

        screenShot.filterMode = FilterMode.Point;
        screenShot.Apply();

        if (Camera.main.orthographic == true)
        {
            Sprite mySprite = Sprite.Create(screenShot, new Rect(0.0f, 0.0f, screenShot.width, screenShot.height), new Vector2(0f, 1f), 100.0f, 0, SpriteMeshType.FullRect);
            GameObject go = new GameObject("test");
            SpriteRenderer sr = go.AddComponent<SpriteRenderer>() as SpriteRenderer;
            //sr.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);
            sr.sprite = mySprite;
            go.transform.rotation = Camera.main.transform.rotation;
            go.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(a.position.x, Screen.height - a.position.y, 5));
        }
        else
        {
            Sprite mySprite = Sprite.Create(screenShot, new Rect(0.0f, 0.0f, screenShot.width, screenShot.height), new Vector2(0f, 1f), 100.0f, 0, SpriteMeshType.FullRect);
            GameObject go = new GameObject("test");
            SpriteRenderer sr = go.AddComponent<SpriteRenderer>() as SpriteRenderer;
            //sr.color = new Color(0.9f, 0.9f, 0.9f, 1.0f);
            sr.sprite = mySprite;
            go.transform.rotation = Camera.main.transform.rotation;
            go.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(a.position.x, Screen.height - a.position.y, 2.6f));
        }
    }

  public static Rect GUIRectWithObject(GameObject go)
    {
        Vector3 cen = go.GetComponent<Renderer>().bounds.center;
        Vector3 ext = go.GetComponent<Renderer>().bounds.extents;

        Vector2[] extentPoints = new Vector2[8]
         {
               WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z-ext.z)),
               WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z-ext.z)),
               WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z+ext.z)),
               WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z+ext.z)),
               WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z-ext.z)),
               WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z-ext.z)),
               WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z+ext.z)),
               WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z+ext.z))
         };
        Vector2 min = extentPoints[0];
        Vector2 max = extentPoints[0];
        foreach (Vector2 v in extentPoints)
        {
            min = Vector2.Min(min, v);
            max = Vector2.Max(max, v);
        }
        return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
    }

    public static Vector2 WorldToGUIPoint(Vector3 world)
    {
        Vector2 screenPoint = Camera.main.WorldToScreenPoint(world);
        screenPoint.y = (float)Screen.height - screenPoint.y;
        return screenPoint;
    }
}