首页 文章

无法在屏幕上拖动精灵时找到一种方法来夹住鼠标的位置

提问于
浏览
0

嗨我有一个精灵在鼠标左键单击屏幕上移动,我的问题是精灵可以被拖离场景 . 我相信使用mathf和clamp但是无法想象如何将它插入到这个脚本中 .

尝试在sprite上插入一个基本的mathf.clamp脚本,但结果是精灵会快速地在我的夹子的顶部和底部之间轻弹 .

using System.Collections;

使用System.Collections.Generic;使用UnityEngine;

公共类DragMove:MonoBehaviour {

public GameObject gameObjectToDrag; // refer to Go that being dragged

public Vector3 Gocenter; // gameobject centre
public Vector3 touchPosition; // touch or click position
public Vector3 offSet; // vector between touchpoint/mouse click to the object centre
public Vector3 newGOCenter; // new center of object

RaycastHit hit; // store hit object information

public bool draggingmode = false; //   



// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update()
{

    //********************************
    // **Click to Drag****************
    //********************************

如果UNITY_EDITOR

感谢您的任何帮助

1 回答

  • 0

    我用了这段代码

    if (newGOCenter.y <= 1)
                        {
                            newGOCenter.y = 1;
                        }
                        if (newGOCenter.y >= 5)
                        {
                            newGOCenter.y = 5;
                        }
                        if (newGOCenter.x <= 1)
                        {
                            newGOCenter.x = 1;
                        }
                        if (newGOCenter.x >= 9)
                        {
                            newGOCenter.x = 9;
                        }
    

相关问题