unity 鼠标悬停事件

5 篇文章 0 订阅

笔者在网上发现了,很多种方法 ,当然咱们找最好用的,也简单的 下面废话不多说直接上代码  我在啰嗦几句 第一这个脚本挂在需要相应的游戏体上  第二被挂游戏体必须带有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);
// }}}
 
 
 
 
 
 
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值