例如,我想从游戏对象的中心获取鼠标单击的位置,例如 Sphere of Scale(1, 1, 1)。如果我单击球体的中心,它应该将 x 分量返回为零,在单击球体的最左侧时,它应该返回 -0.5 作为 vector3 的 x 分量,在单击球体的最右侧时返回 0.5 . 下面的代码帮助我在起源时实现这一点。但是,它有一个限制条件。球体必须定位在(0,任何东西,任何东西)(因为我关心的是 x 轴)。无论球体位置如何,我如何才能实现这一目标的任何帮助?
bool isGameOver = false;
float pointX;
// Update is called once per frame
void Update () {
if(!isGameOver){
RaycastHit hit;
if(Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit)){
if(hit.transform.tag =="Ball"){
pointX = transform.InverseTransformPoint(hit.point).x;
}
}
}
}
}