[英]I want to move 2d object only horizontally by touch in Unity
我實現了觸摸拖動。
但是我不能只水平移動。
我希望將其限制為是否,但是在那之后我不知道該怎么辦。
這是我的代碼。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
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 ("OnBeginDrag");
//this.transform.position = eventData.position;
GetComponent<Transform> ().position = eventData.position;
}
public void OnEndDrag (PointerEventData evnetData)
{
Debug.Log ("OnEndDrag");
}
}
您無需調用GetComponent進行轉換。 如果您使用的代碼有效,並且只想水平拖動,則類似這樣的操作應該起作用。
var currentPos = transform.position;
transform.position = new Vector3(eventData.position.x, currentPos.y, currentPos.z);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.