查看: 1422|回复: 3
打印 上一主题 下一主题

unity3d鼠标跟随代码杂集,仅供参考,需检验

[复制链接]

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-8-3 17:45:13 |只看该作者 |倒序浏览
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 = ***e;

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);

}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2508

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32806
精华
12

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-8-3 17:54:25 |只看该作者
我爱纳金网~www.narkii.com
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-12-30 15:45:10 |只看该作者
学习了,虽然还是有难度,谢谢楼主的用心  
回复

使用道具 举报

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38268
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-12-31 01:51:29 |只看该作者
谢谢楼主的帖子分享,学习了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-23 23:59 , Processed in 0.091379 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部