Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用鼠标点击移动精灵_C#_Unity3d - Fatal编程技术网

C# 用鼠标点击移动精灵

C# 用鼠标点击移动精灵,c#,unity3d,C#,Unity3d,嗨,我遵循了一个关于如何通过拖动移动精灵的utube教程,但我无法让它工作 我是unity的新手,如果这有点简单,我很抱歉 这是附在我的主相机上的脚本,我已经在我的精灵上贴了一个胶囊胶 感谢您在高级课程中的帮助 下一步是添加触摸输入 using System.Collections; using System.Collections.Generic; using UnityEngine; public class DragMove : MonoBehaviour { publ

嗨,我遵循了一个关于如何通过拖动移动精灵的utube教程,但我无法让它工作

我是unity的新手,如果这有点简单,我很抱歉

这是附在我的主相机上的脚本,我已经在我的精灵上贴了一个胶囊胶

感谢您在高级课程中的帮助

下一步是添加触摸输入

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class 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****************
    //********************************

    // first frame when user click left button

    if (Input.GetMouseButtonDown(0))
    {
        // convert mouse position to ray
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        // if ray hit a collider (not 2dcollider)
        if (Physics.Raycast(ray, out hit))
        {
            gameObjectToDrag = hit.collider.gameObject;
            Gocenter = gameObjectToDrag.transform.position;
            touchPosition = Camera.main.ScreenToWorldPoint
(Input.mousePosition);
            offSet = touchPosition - Gocenter;
            draggingmode = true;
        }
    }

    // every frame when user hold left mouse
    if (Input.GetMouseButtonDown(0))
    {
        if (draggingmode)
        {
            touchPosition = Camera.main.ScreenToWorldPoint
(Input.mousePosition);
            newGOCenter = touchPosition - offSet;
            gameObjectToDrag.transform.position = new Vector3(newGOCenter.x, 
newGOCenter.y, newGOCenter.z);
        }
    }
    if (Input.GetMouseButtonUp(0))
    {
        draggingmode = false;
    }

    }
}

GetMouseButtonDown仅在第一帧返回true。用于检测何时按下鼠标按钮

// every frame when user hold left mouse
if (Input.GetMouseButtonDown(0))
{
应该改为

// every frame when user hold left mouse
if (Input.GetMouseButton(0))
{

我点击了“怨恨”,但没有移动。你确定这是一个精灵吗?请发布该对象的“Inspector”选项卡截图