【问题标题】:C# - On Mouse Drag Game Object follows Mouse on a single Axis. 2DC# - 鼠标拖动游戏对象在单个轴上跟随鼠标。二维
【发布时间】:2016-08-21 16:00:02
【问题描述】:

我正在尝试创建一个 2d 游戏,当用户拖动对象时,游戏对象应在单个轴(x 轴)上跟随鼠标。

  1. 起点

  2. 当我移动时,它不应该上升或下降。

这是我的代码,问题是,游戏对象在我拖动的任何地方都跟随鼠标。

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

【问题讨论】:

    标签: c# unity3d drag-and-drop


    【解决方案1】:

    您当前正在使用this.transform.position = eventData.position; 更改所有轴(x,y,z)。仅更改 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;
    }
    

    【讨论】:

    • Vector3 嗯,我应该探索更多(y)。我能再问一个问题吗?是否也可以在拖动时放慢速度并且有限制,因为我将它作为重物拖动。
    • 我不知道你说的放慢速度是什么意思。由于这看起来像不同的问题,为什么不创建一个新问题然后在此处提供链接。我去看看。
    • [link] (stackoverflow.com/questions/39083167/…) 好了,先生。 (大师)。
    猜你喜欢
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多