2017-03-27 13 views
0

SteamVRコントローラとレイキャストでクリックしてドラッグすることで、ローカルスペース内のUI要素のy位置を移動しようとしています。私は予測できない結果になっているように見えます。Rayキャストを使用してオブジェクトを移動する

私は、ドラッグの開始時にレイキャストの位置を取得し、その位置とドラッグ中に開始された場所との間の距離を移動しようとしています。ここで

は私のコードです:

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

答えて

0

あなたは.MoveTowardsに.MovePositionを変換することができます。しかしそれはまだ周りを飛び越えた。コードが右クリックされたフレームでのみ実行されていたことが判明したので、ifステートメントから移動してください。

メインカメラに配置されたスクリプト全体を示します。あなたは常にターゲットを選択する必要があります。エラーを防ぐために、剛体を持つgameObjectを "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; 
    } 
} 
} } 
+1

私はマウスではなくゲームオブジェクトから来るレイキャストでそれをしようとしています。特にSteamVRコントローラ。私はトリガーを押し続け、そのオブジェクトをそのy軸のローカル空間にドラッグしたいと思います。 – OT2O

+0

こんにちはOT2O、StreamVRをインストールして戻ってみましょう –

+0

あなたは運がありましたか? – OT2O

関連する問題