Unity调试日志全浮动

时间:2016-09-29 15:01:46

标签: c# unity3d

我很困惑,这是我创建并记录浮点数的尖锐脚本:

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 }?如果以前的帖子回答了这个问题,请原谅我并给我链接:)。

1 个答案:

答案 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;