您的位置:首页 > 移动开发 > Unity3D

Unity 鼠标拖动场景内的物体

2015-07-24 10:04 671 查看
using UnityEngine;
using System.Collections;

public class mouse_Drag : MonoBehaviour {

public Camera mCamera;
public float deth = 10f;
//鼠标放到物体上
void OnMouseEnter()
{
this.transform.localScale = new Vector3(1.3f,1.3f,1.3f);
}

//鼠标在物体上移除的时候
void OnMouseExit()
{
this.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
}
//鼠标放在物体上一直触发
void OnMouseOver()
{
this.transform.Rotate(Vector3.up,45 * Time.deltaTime,Space.Self);
}

void OnMouseDrag()
{
//moveObject();
moveObject_fixdepth();
}

void moveObject()
{
//把鼠标点坐标转换为世界坐标,并发出一条射线
Ray r = mCamera.ScreenPointToRay(Input.mousePosition);
//采集射线打到的物体信息
RaycastHit hit;
//设置射线长度为1000
if(Physics.Raycast(r,out hit, 1000f,1))
{
this.transform.position = new Vector3(hit.point.x,hit.point.y,hit.point.z);
Debug.DrawLine(r.origin,hit.point,Color.red);
}

}

void moveObject_fixdepth()
{
Vector3 mouseScreen = Input.mousePosition;
mouseScreen.z = deth;
Vector3 mouseWorld = mCamera.ScreenToWorldPoint(mouseScreen);
this.transform.position = mouseWorld;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: