'픽킹'에 해당되는 글 1건

  1. 2015.12.18 [Unity] Picking
Game/Unity2015.12.18 15:28

<마우스 좌표에 오브젝트 붙이기>   


IEnumerator OnMouseDown()

    {

        Vector3 scrSpace = Camera.main.WorldToScreenPoint(transform.position);//객체의 스크린좌표를 구하고       

        Vector3 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, scrSpace.z));


        while (Input.GetMouseButton(0))

        {

            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, scrSpace.z);//현재 스크린좌표를 구함

            Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset; //마우스좌표를 월드좌표로 + 

            transform.position = curPosition;

            yield return null;

        }

    }


 


픽킹


RaycastHit m_Hit;


if ( Input.GetMouseButtonDown(0) == true ) //마우스를 눌르면

{

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //현재 마우스클릭한 위치


if ( Physics.Raycast(ray,out m_Hit,100)) // 피킹이 되면 m_Hit에 피킹된 오브젝트정보가 달려온다.

{

Debug.Log(m_Hit.transform.tag); 

}

}


 

//////////////////////////////////////////////////////////////////////////////


using UnityEngine;

using System.Collections;


public class picking : MonoBehaviour

{


    protected Ray ray;

    protected RaycastHit hit;

   


    // Use this for initialization

    void Start()

    {


    }


    // Update is called once per frame

    void Update()

    {


        if (Input.GetMouseButton(0) == true)

        {

            ray = Camera.main.ScreenPointToRay(Input.mousePosition);


            if (Physics.Raycast(ray, out hit))

            {

                Vector3 offset;

                Vector3 scrSpace = Camera.main.WorldToScreenPoint(hit.transform.position);

                offset = hit.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, scrSpace.z));


                Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, scrSpace.z);

                Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace); // +offset;

                hit.transform.position = curPosition;


                Debug.Log(curScreenSpace.x);

                Debug.Log(curPosition.x);

            }

        }

    }


저작자 표시
신고
Posted by Namseungil