2016-08-21 12 views
0

私は2dゲームを作成しようとしています。ユーザーがオブジェクトをドラッグしたときに、ゲームオブジェクトは1軸(x軸)上でマウスに追従する必要があります。私はそのはずのが上下に行くか、または移動するとき C# - マウスドラッグ時ゲームオブジェクトは、1つの軸上でマウスをたどります。 2D

  • ポイントenter image description here

  • 開始

    1. enter image description here

    ここでの問題は、私はドラッグどこでもゲームオブジェクトがマウスを追従している、である、私のコードです。

    using UnityEngine; 
    using System.Collections; 
    using UnityEngine.EventSystems; 
    
    public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { 
    
        public void OnBeginDrag(PointerEventData eventData) 
        { 
         Debug.Log("OnBeginDrag"); 
        } 
    
        public void OnDrag(PointerEventData eventData) 
        { 
         Debug.Log("OnDrag"); 
    
         this.transform.position = eventData.position; 
        } 
    
        public void OnEndDrag(PointerEventData eventData) 
        { 
         Debug.Log("OnEndDrag"); 
        } 
    } 
    

    答えて

    1

    現在、すべての軸(x、y、z)をthis.transform.position = eventData.position;で変更しています。 x軸のみを変更します。 eventData.position.xのみを使用してください。

    public void OnDrag(PointerEventData eventData) 
    { 
        Debug.Log("OnDrag"); 
        Vector3 tempPos = this.transform.position; 
        tempPos.x = eventData.position.x; 
        this.transform.position = tempPos; 
    } 
    
    +0

    ベクター3より多くの(y)を探索する必要があります。別の質問をすることはできますか?ドラッグしている間に速度を落として制限することも可能です。重いオブジェクトとしてドラッグすることができます。 – Perfectionist

    +0

    私はあなたがそれを遅くすることによって何を意味するのか分かりません。これは異なる質問のように見えるので、ここで新しい質問を作成してリンクを提供してみませんか?私は見てみましょう。 – Programmer

    +0

    [link](http://stackoverflow.com/questions/39083167/c-sharp-drag-and-then-drop-in-specified-area) これはここにあります。 (マスター)。 – Perfectionist

    関連する問題