var depth = 10.0;
var go:gameObject;
function Start ()
{
Screen.showCursor = false;
}
function Update ()
{
var mousePos = Input.mousePosition;
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
offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, screenSpace.z));
}
/*
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))
return;
// We need to hit a rigidbody.
if (!hit.rigidbody)
return;
chicken = hit.rigidbody.gameObject;
origPos = chicken.transform.position;
StartCoroutine ("DragObject");
}
IEnumerator DragObject()
{
while (Input.GetMouseButton (0))
{
// 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
newMousePos -= transform.position;
chicken.transform.Translate(curMousePos - newMousePos);
curMousePos = newMousePos;
yield return 0;
}
chicken.transform.position = origPos;
chicken = null;
}
}
//下面这几段独立代码经检验可用===========================================
版本1:可改进
// CursorIcon.js
// Place this script onto the model you want to follow the cursor around.
enum Type {X, Y, XandY}
var type = Type.XandY;
// This is only used if you are using Type.Y. It is the x position of it.
var xSpot = 0.0;
// This is always used.
var depth = 0.0;
// This is only used if you are using Type.X. It is the y position of it.
var height = 0.0;
private var helperCollider : GameObject;
function Start ()
{
Screen.showCursor = false;
helperCollider = GameObject ("HelperCollider");
helperCollider.AddComponent (BoxCollider);
helperCollider.collider.isTrigger = true;
helperCollider.transform.localScale = Vector3 (50, 50, 0);
helperCollider.transform.parent = camera.main.transform;
helperCollider.transform.position = Vector3 (0, 0, depth);
if (collider)
Destroy (collider);
}
function Update ()
{
var ray : Ray = camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
if (hit.collider == helperCollider.collider)
{
if (type == Type.X)
transform.position = Vector3 (hit.point.x, height, depth);
else
if (type == Type.Y)
transform.position = Vector3 (xSpot, hit.point.y, depth);
else
transform.position = Vector3 (hit.point.x, hit.point.y, depth);
}
}
}
版本2:简洁
var distanceFromCamera : float;
private var X : int;
private var Y : int;
// position a 3D object where the mouse cursor would be
function Update () {
Screen.showCursor = false;
X = Input.mousePosition.x;
Y = Input.mousePosition.y;
transform.position = camera.main.ScreenToWorldPoint(Vector3(X,Y,distanceFromCamera));
}
版本3:1与2的结合
enum Type {X, Y, XandY}
var type = Type.XandY;
// This number ranges between 0 and 1.
// 1 is at the right of the screen, and 0 is a the left of the screen.
// This is only used if you are using Type.Y.
var xOffset = 0.5;
// This number ranges between 0 and 1.
// 1 is at the top of the screen, and 0 is a the bottom of the screen.
// This is only used if you are using Type.X.
var height = 0.5;
// This is the z distance from the camera.
var depth = 10.0;
function Start ()
{
Screen.showCursor = false;
}
function Update ()
{
var x = Input.mousePosition.x;
var y = Input.mousePosition.y;
var mainCam = camera.main;
if (type == Type.X)
transform.position = mainCam.ScreenToWorldPoint (Vector3 (x, height * Screen.height, depth));
else
if (type == Type.Y)
transform.position = mainCam.ScreenToWorldPoint (Vector3 (0, y, depth));
else
transform.position = mainCam.ScreenToWorldPoint (Vector3 (x, y, depth));
height = Mathf.Clamp01 (height);
xOffset = Mathf.Clamp01 (xOffset);
}