Created
May 6, 2015 13:10
-
-
Save romainPechot/3cb4937a693bd7cc8ec5 to your computer and use it in GitHub Desktop.
Move Gameobject with mouse click relative to camera.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class MouseClickPosition : MonoBehaviour | |
{ | |
public Camera cam; | |
public Transform target; | |
public bool useRaycast = false; | |
public LayerMask maskFilter; | |
private RaycastHit hit; | |
public float distanceToCamera = 10f; | |
private void Update() | |
{ | |
if(Input.GetMouseButtonDown(0)) | |
{ | |
if(useRaycast) | |
{ | |
// hit ? | |
if(Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, distanceToCamera, maskFilter)) | |
{ | |
// move to position | |
target.position = hit.point; | |
} | |
} | |
else | |
{ | |
// move to position | |
target.position = cam.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * distanceToCamera); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment