0

我正在尝试通过单击并拖动 SteamVR 控制器和光线投射来移动 UI 元素在其本地空间中的 y 位置。我得到了在我看来是不可预测的结果。

我试图在拖动开始时获取光线投射的位置,并将其移动到拖动时它所在位置与开始位置之间的距离。

这是我的代码:

if (hit.transform.name == "Content" && scrollSet == false)
{
    content = hit.transform;
    scrollSet = true;
    scrollPos = hit.transform.position ;
}

if (scrollSet == true)
{
    if (rController.triggerPressed)
    {
        y = hit.transform.position.y - scrollPos.y;
        content.transform.localPosition = new Vector3(content.transform.localPosition.x,  content.localPosition.y + y, content.transform.localPosition.z);
    }
    else
    {
        scrollSet = false;
    }
}
4

1 回答 1

0

您可以将 .MovePosition 转换为 .MoveTowards。但它还是跳来跳去。事实证明,代码仅在您右键单击的帧期间执行,因此将其移出 if 语句。

这是放置在主摄像机中的整个脚本。您总是需要选择一个目标,因此为了防止错误,您需要将一个带有刚体的游戏对象放入“bTarg”中。

public class ClickTarget : MonoBehaviour {

private GameObject target; private Vector3 destination; private float distance; private Vector3 tTrans;

public GUIText targetDisplay; public float speed; public GameObject bTarg;

void Start () { targetDisplay.text = ""; distance = 0.0f; target = bTarg; }

void Update () { if(Input.GetButtonDown("Fire1")){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit)){ if(hit.collider != null){ if(hit.collider.tag == "Unit"){ target = hit.collider.gameObject; targetDisplay.text = "Unit: " + hit.collider.gameObject.name; destination = target.transform.position; target.rigidbody.freezeRotation = false; } if(hit.collider.tag == "Building"){ target = hit.collider.gameObject; targetDisplay.text = "Building: " + hit.collider.gameObject.name; } } } } }

void FixedUpdate(){ if (Input.GetButtonDown ("Fire2") && target.tag == "Unit" && GUIUtility.hotControl == 0) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray,out hit)){ destination = hit.point; } }

 tTrans = target.transform.position;
 distance = Vector3.Distance (tTrans, destination);
 if(target.tag == "Unit"){
     if (distance > .2f) {
         target.transform.LookAt (destination);
         target.transform.position = Vector3.MoveTowards (target.transform.position, destination, speed);
         target.rigidbody.freezeRotation = true;
     }
 }
} }
于 2017-03-28T00:12:10.123 回答