#Assets
I have included all the necessary information from my project.
#The Intended Goal
I think it becomes obvious what I am trying to do; I am trying to hover a cursor on each solar system object that renders on the screen. And then I track it each update.
#Problems:
—it seems like my problems really started when I switched the Canvas Render-Scaling to: “Keep pixel constant”, now it seems like the coordinate systems are not transferring appropriately… OR, I am not understanding the anchors enough.
—Also I don’t understand the logic behind using the Sun’s local coordinates… or it’s world coordinates to obtain it’s (WorldToScreenPoint). This code seemed to look better when I was using the sun’s local coordinates… But in my mind it doesn’t make sense because the camera should care more if the sun is at -8000,0,0… instead of it’s local 0,0,0
— on the Sprite PreFAB, I seem to switch and try all kinds of things to get this damn thing to work. I have generally understand the difference between keeping a pivot anchor, and stretching… I guess my biggest mis-understanding was how when choosing the “BOTTOM_LEFT” pivot. Before when my Canvas_scaler was on “Scale with screen size” everything seemed to work pretty good, sort of… But, once I switched to “Constant pixel size”. It’s as if the x,y positioning is somehow scaled to the screen? or something…( not just the literal scale )



#SpriteHudBehavior script ( on the SpriteHudLayer, a CANVAS_PANEL )
using UnityEngine;
using System.Collections;
using System;
public class SpriteHudBehavior : MonoBehaviour {
public LayerMask layerToCheckForObjects;
public float distanceToPrepSprite;
public GameObject playerToTrack;
public GameObject spriteToUse;
public bool checkAgain;
public Collider[] trackedEnvironmentColliders;
public Vector3 copyPosition;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (checkAgain) // WE CHECK AGAIN ANYTIME we warp into a NEW SOLAR SYSTEM
{
checkAgain = false;
Array.Clear (trackedEnvironmentColliders,0,trackedEnvironmentColliders.Length);
Vector3 thePlayersPosition = playerToTrack.transform.position;
trackedEnvironmentColliders = Physics.OverlapSphere(thePlayersPosition,distanceToPrepSprite, layerToCheckForObjects.value);
}
//ALWAYS RUN THIS
foreach (Collider eachCollider in trackedEnvironmentColliders)
{
copyPosition = eachCollider.gameObject.transform.position;
copyPosition = transform.TransformPoint (copyPosition);
copyPosition = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent <screencoor> ().GiveMeBackValuesfor(copyPosition);
if (copyPosition.x > 0f && copyPosition.y > 0f && copyPosition.x < Screen.width && copyPosition.y < Screen.height && copyPosition.z <0f)
{
//if cursor != exist, then create one
if (eachCollider.GetComponent <EnvironmentFoundation> ().myCursorIsOnScreen == false)
{
GameObject copyOf = Instantiate (spriteToUse, new Vector3 (0f,0f,0f), Quaternion.identity) as GameObject;
copyOf.transform.SetParent (this.transform); // Once Instantiated, set it's parent to the HUDwindow ( this script)
copyOf.GetComponent <RectTransform> ().anchoredPosition3D = new Vector3 (copyPosition.x, copyPosition.y,0f); // Before the frame updates, set it's position to the correct position
//copyOf.GetComponent <RectTransform> ().localScale = new Vector3 (1f,1f,1f); //Using the Canvas-Render "constant pixel size", when we do the transform is also does scale, but we need to keep it original
copyOf.name = "Cursor"; // Before the frame updates, set it's name to Cursor
copyOf.tag = "Cursor"; // Before the frame updates, Set it's tag
copyOf.layer = 9; // Before the frame updates, Set this Sprite to a Layer only visible by the UI
copyOf.GetComponent <CursorTracker> ().myOwner = eachCollider.gameObject; // The cursor needs to remember the object that it gets info from
eachCollider.GetComponent <EnvironmentFoundation> ().myCursorIsOnScreen = true;
}
// So if all cursors are on the screen that need to be:
foreach (GameObject c in GameObject.FindGameObjectsWithTag ("Cursor"))
{
copyPosition = GameObject.FindGameObjectWithTag ("MainCamera").GetComponent <screencoor> ().GiveMeBackValuesfor(copyPosition);
c.GetComponent <RectTransform> ().anchoredPosition3D = new Vector3 (copyPosition.x, copyPosition.y,0f);
}
} else
{
// otherwise if the object is off screen
// THEN: we need to make sure that if has a cursor, it is deleted
if (eachCollider.GetComponent <EnvironmentFoundation> ().myCursorIsOnScreen == true)
{
eachCollider.GetComponent <EnvironmentFoundation> ().myCursorIsOnScreen = false;
foreach (GameObject c in GameObject.FindGameObjectsWithTag ("Cursor"))
{
if (c.gameObject.GetComponent <CursorTracker> ().myOwner != eachCollider.gameObject){continue;}
c.GetComponent <CursorTracker> ().Please86Yourself ();
}
}
}
}
}
}
#Screencoord script (on camera)
using UnityEngine;
using System.Collections;
public class screencoor : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public Vector3 GiveMeBackValuesfor (Vector3 objectPassTransform)
{
return camera.WorldToScreenPoint (objectPassTransform);
}
}
#Environment foundation ( put on all the suns and planets)
using UnityEngine;
using System.Collections;
public class EnvironmentFoundation : MonoBehaviour {
public bool myCursorIsOnScreen;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
#CursorTracker script (placed on each instantiated cursor)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CursorTracker : MonoBehaviour {
public GameObject myOwner;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
}
public void Please86Yourself ()
{
Destroy (gameObject);
}
}