笔者在网上发现了,很多种方法 ,当然咱们找最好用的,也简单的 下面废话不多说直接上代码 我在啰嗦几句 第一这个脚本挂在需要相应的游戏体上 第二被挂游戏体必须带有collider, 第三仅仅制作完上面的两步 本应该没有问题,,笔者又发现一个问题 就是只有鼠标在物体的右上方才会很灵敏的相应到 在在左下方反而没什么反应 ,为此笔者在脚本上加上了一句
this.GetComponent<BoxCollider> ().size = new Vector3 (1.5f, 1.5f, 1.5f);
原来物体的size 为(1,1,1) 我的目的是加大物体本身的碰撞体 结果顺利的实现了
下面是完整
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Shubiaoxuanting : MonoBehaviour{ bool ischanger; //上移动 0为未上移 1为上移 int move_up=0; void Start () { ischanger = false; //扩大碰撞体的尺寸 方便检测鼠标悬停 this.GetComponent<BoxCollider> ().size = new Vector3 (1.5f, 1.5f, 1.5f); //给麻将初始状态的位置 this.transform.position = new Vector3 (this.transform.position.x, 0, this.transform.position.z); } //鼠标在物体上面引起的动作 void OnMouseOver(){ ischanger = true; this.transform.position = new Vector3 (this.transform.position.x, 0.2f, this.transform.position.z); } // void OnMouseEnter(){ // ischanger = true; // move_up = 1; // Debug.Log ("3333333"); // // } //鼠标不再上面引起的动作 void OnMouseExit(){ ischanger = false; this.transform.position = new Vector3 (this.transform.position.x, 0, this.transform.position.z); } /鼠标按下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shubiaoxuanting : MonoBehaviour{
bool ischanger; //上移动 0为未上移 1为上移
int move_up=0;
void Start () { ischanger = false; //扩大碰撞体的尺寸 方便检测鼠标悬停
this.GetComponent<BoxCollider> ().size = new Vector3 (1.5f, 1.5f, 1.5f);
//给麻将初始状态的位置 this.transform.position = new Vector3
(this.transform.position.x, 0, this.transform.position.z); }
//鼠标在物体上面引起的动作 void OnMouseOver(){ ischanger = true;
this.transform.position = new Vector3 (this.transform.position.x, 0.2f,
this.transform.position.z); }
// void OnMouseEnter(){
// ischanger = true;
// move_up = 1;
// Debug.Log ("3333333");
// // }
//鼠标不再上面引起的动作
void OnMouseExit(){
ischanger = false;
this.transform.position = new Vector3 (this.transform.position.x, 0, this.transform.position.z);
} /鼠标按下
/鼠标按下
void OnMouseDown(){
}
}
//鼠标松开
//鼠标松开
void OnMousePut(){}
void OnMousePut(){}
void Update () {
// if (ischanger){
//Debug.Log ("33333");
//this.transform.position = new Vector3 (this.transform.position.x, 0.2f, this.transform.position.z)
} else {
// this.transform.position = new Vector3 (this.transform.position.x, 0, this.transform.position.z);
// }}}
因篇幅问题不能全部显示,请点此查看更多更全内容