我很困惑,这是我创建并记录浮点数的尖锐脚本:
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
-3
是coordinateytomove
,它不会从当前坐标更改,但为什么coordinatextomove
发生了更改,其调试记录为0
而不是-2.99853587116
}?如果以前的帖子回答了这个问题,请原谅我并给我链接:)。
答案 0 :(得分: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;