Unity/유니티 개발
screenToWorldPoint 마우스 컨트롤 위치로 gameobject움직이기
도이(doi)
2021. 8. 6. 17:11
728x90
카메라 모드를 orthographic으로 해두어야 된다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mousePosInteractor : MonoBehaviour
{
private Vector2 cursorPos;
void Update()
{
transform.position = new Vector3(cursorPos.x, cursorPos.y, transform.position.z);
}
void OnGUI()
{
Vector3 p = new Vector3();
Camera c = Camera.main;
Event e = Event.current;
Vector2 mousePos = new Vector2();
mousePos.x = e.mousePosition.x;
mousePos.y = c.pixelHeight - e.mousePosition.y;
p = c.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, c.nearClipPlane + 14));
cursorPos.x = p.x;
cursorPos.y = p.y;
}
}
728x90
반응형