2017-10-17 2 views
-1

안녕하세요 저는 드래그하여 스프라이트를 움직이는 방법에 대한 utube 튜토리얼을 따랐지만 작동시키지 못했습니다. 나는이 단순한 비트가 정말 미안 단결에 새로운 오전마우스 클릭으로 스프라이트 이동

이 내 메인 카메라에 장착 된 스크립트, 난

는 당신의 도움을 주셔서 감사합니다 내 스프라이트에 캡슐 colllider를 첨부 고급

다음 단계는

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

    } 
} 
+0

을 읽어야하지만이 스프라이트입니다 확실 –

+0

를 이동하지 않는 이유는 무엇입니까? 해당 개체의 "Inspector"탭 스크린 샷을 게시하십시오 – Programmer

답변

0

GetMouseButtonDown은 첫 번째 프레임 만 true를 돌려 터치 입력을 추가하는 것입니다. GetMouseButton은 마우스 버튼을 눌렀을 때를 감지하는 데 사용됩니다.

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

대신 나는에도 불구을 클릭

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

부탁드립니다. –