var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
print(wantedPos);
}
//-------------------------------------------
function Update () {
}
var screenSpace;
var offset;
function OnMouseDown(){
//translate the cubes position from the world to Screen Point
screenSpace = Camera.main.WorldToScreenPoint(transform.position);
//calculate any difference between the cubes world position and the mouses Screen position converted to a world point
OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
OnMouseDrag is called every frame while the mouse is down.
*/
function OnMouseDrag () {
//keep track of the mouse position
var curScreenSpace = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//convert the screen mouse position to world point and adjust with offset
var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
//update the position of the object in the world
transform.position = curPosition;
}
//========================================
/*
Notes:
This script should be attached to the camera, so we can use the "camera" member variable with needing a "FindCamera" script.
*/
using UnityEngine;
using System.Collections;
public class ChickenMove : MonoBehaviour
{
Vector3 origPos, curMousePos;
GameObject chicken;
void Update()
{
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown(0))
return;
curMousePos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
// Position relative to the eye-point of the camera
curMousePos -= transform.position;
// We need to actually hit an object
RaycastHit hit;
// BJL: If we use a collider instead of a rigidbody and the distance parameter is too large, we'll get a hit all the time.
// Could this be due to reflection or because the camera is orthographic, or...?
if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, 100))
// Position on the near clipping plane of the camera in world space
Vector3 newMousePos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
// Position relative to the eye-point of the camera