2016-09-29 49 views
0

我糊涂了,这是我的创造,并记录浮动升C脚本:统一调试日志全浮

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); 

     } 

    } 
} 

当我在计算器手动计算的话,的coordinatextomove结果是:

-2.99853587116

,但在调试日志是:

-3 0

-3coordinateytomove不从目前的变化的坐标,但为什么如果coordinatextomove改变,其调试记录0代替-2.99853587116?如果有以前的帖子回答了这个问题,请原谅我,并给我链接的帖子:)。

+0

如果你想在你可能想要做的'camera.WorldToScreenPoint(theobject.transform.position)屏幕的面积来选择对象;',让你的'coordinatex',并从'coordinatey'。这会给你游戏对象屏幕上的x和y坐标。 –

回答

2

screenheight/selectedheight会给你整数除法,在得到结果后忽略.右边的任何内容。你需要投两中的一个作为浮动你把

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

之前,你也可以只声明变量float和以后不必做演员。

int selectedwidth = 1366; 
int selectedheight = 768; 
float screenwidth = Screen.width; 
float screenheight = Screen.height; 
+0

标记为答案! :) – user6668201

+0

有关更多信息,请参阅http://stackoverflow.com/questions/10851273/why-integer-division-in-c-sharp-returns-an-integer-but-not-a-float。 –

+0

谢谢,只需等待8-9分钟作答标记 – user6668201