From bfb8b332a38e318f5ae3da796a6ee8b0ed3998cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ece=20Sefercio=C4=9Flu?= Date: Sun, 25 Oct 2020 19:51:56 +0300 Subject: [PATCH] Create MoveWithMouseDrag.cs Allows translation with mouse drag in 3D space --- MoveWithMouseDrag.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 MoveWithMouseDrag.cs diff --git a/MoveWithMouseDrag.cs b/MoveWithMouseDrag.cs new file mode 100644 index 0000000..3b2ec75 --- /dev/null +++ b/MoveWithMouseDrag.cs @@ -0,0 +1,30 @@ +using System; +using UnityEngine; + +[RequireComponent(typeof(Collider))] +public class MoveWithMouseDrag : MonoBehaviour +{ + private Camera mainCamera; + private float CameraZDistance; + + void Start() + { + mainCamera = Camera.main; + CameraZDistance = + mainCamera.WorldToScreenPoint(transform.position).z; //z axis of the game object for screen view + } + + private float counter = 0; + + void OnMouseDrag() + { + Vector3 ScreenPosition = + new Vector3(Input.mousePosition.x, Input.mousePosition.y, CameraZDistance); //z axis added to screen point + Vector3 NewWorldPosition = + mainCamera.ScreenToWorldPoint(ScreenPosition); //Screen point converted to world point + + transform.position = NewWorldPosition; + } + + +}