x


Camera Relative Screen Pixel-coordinates

I've been trying to set up a Domino-Line (parent object) with 5 Dominos in it (Children). When mousing over the Domino-Line, I want two UI-elements to show up: A '-' sign behind the first domino in line, and a '+' behind the last in line.

However, when trying to use WorldToScreenPoint, the UI-elements get placed all wrong, to the left of the entire line above eachother.

So how would I fix this?

EDIT: Here's part of the code, the OnGUI function

void OnGUI() {
    if (playScript.gamePlays == false) {
        if (mouseOver == true) {

            Vector3 screenPosBegin = Camera.main.WorldToScreenPoint(transform.TransformPoint(firstStone.transform.localPosition));
            Vector3 screenPosEnd = Camera.main.WorldToScreenPoint(transform.TransformPoint(lastStone.transform.localPosition));

            if (amountOfStones > minStones) {
                if (GUI.Button(new Rect(screenPosBegin.x, Camera.main.pixelHeight - screenPosBegin.y, 50 * (1.1f - (screenPosBegin.z/20)), 50 * (1.1f - (screenPosBegin.z/20))), detractStoneTex)) {
                    amountOfStones--;
                    Destroy(lastStone);

                    touchBox.size = new Vector3( (amountOfStones * 0.5f) - 0.5f, 0.6f, 0.4f);
                    touchBox.center = new Vector3( touchBox.size.x/2, 0, 0);

                }
            }


            if (amountOfStones < maxStones) {
                if (GUI.Button(new Rect(screenPosEnd.x, Camera.main.pixelHeight - screenPosEnd.y, 50 * (1.1f - (screenPosBegin.z/20)), 50 * (1.1f - (screenPosBegin.z/20))), addStoneTex)) {
                    amountOfStones++;
                    GameObject newStone = Instantiate( Resources.Load("Domino"), Vector3.zero, Quaternion.identity) as GameObject;
                    newStone.transform.parent = transform;
                    newStone.transform.localPosition = new Vector3((amountOfStones * 0.5f) - 0.5f, 0, 0);
                    newStone.transform.localRotation = Quaternion.identity;


                    touchBox.size = new Vector3( (amountOfStones * 0.5f) - 0.5f, 0.6f, 0.4f);
                    touchBox.center = new Vector3( touchBox.size.x/2, 0, 0);
                }
            }
        }
    }


}

Right now I've made it to look -sorta- ok, by subtracting the height from it.

more ▼

asked Oct 13 '10 at 06:12 PM

Jordi gravatar image

Jordi
66 9 13 19

Could you post some code so that we can see what might be wrong?

Oct 13 '10 at 06:15 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x25

asked: Oct 13 '10 at 06:12 PM

Seen: 1278 times

Last Updated: Oct 16 '10 at 05:50 PM