0

I confused , this is my c sharp script that create and log the float:

using UnityEngine;
using System.Collections;

public class guicontrol : MonoBehaviour {
    void CalculatePosition(string ObjectName) {
        GameObject theobject = GameObject.Find (ObjectName);
        int selectedwidth = 1366;
        int selectedheight = 768;
        int screenwidth = Screen.width;
        int screenheight = Screen.height;
        float coordinatey = theobject.transform.localPosition.y;
        float coordinatex = theobject.transform.localPosition.x;
        float coordinatez = theobject.transform.localPosition.z;
        Debug.Log ("work at current stats : " + screenwidth + " " + screenheight);
        if (screenwidth != selectedwidth || screenheight != selectedheight) {

            float coordinateytomove = (screenheight / selectedheight) * coordinatey;
            float coordinatextomove = (screenwidth / selectedwidth) * coordinatex;
            Debug.Log (coordinateytomove + " " + coordinatextomove);

        }

    }
}

When I calculate it manually in calculator , the result of coordinatextomove was :

-2.99853587116

but in the debug log was :

-3 0

-3 was the coordinateytomove which don't change from its current coordinate , but why if the coordinatextomove changed , its debug logged 0 instead of -2.99853587116 ? If there's previous post answered this question , forgive me and give me the link of the post :) .

Sinatr
  • 20,892
  • 15
  • 90
  • 319
user6668201
  • 67
  • 3
  • 10
  • If you are trying to select objects in a area of the screen you may want to do `camera.WorldToScreenPoint(theobject.transform.position);` and get your `coordinatex` and `coordinatey` from that. that will give you the x and y coordinates on the screen of the game object. – Scott Chamberlain Sep 29 '16 at 15:13

1 Answers1

4

screenheight / selectedheight will give you integer division ignoring anything to the right of the . after you get a result. You need to cast one of the two as a float before you divide

float coordinateytomove = ((float)screenheight / selectedheight) * coordinatey;
float coordinatextomove = ((float)screenwidth / selectedwidth) * coordinatex;

You could also just declare the variables as float and not need to do the cast later.

int selectedwidth = 1366;
int selectedheight = 768;
float screenwidth = Screen.width;
float screenheight = Screen.height;
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431