unity GUI 多个面板的控制 整板卡位 边界控制

3 篇文章 0 订阅

引:

上个博客中讲述了如何用鼠标拖动面板,但面板的拖动范围并没有得到控制。而且各个面板拖动起来总差点意思,接下来我们通过代码的方式实现两个功能:

  • 左右边界控制
  • 整板卡位

先看效果:

在这里插入图片描述

从图中可以看出,每个面板感觉都想有根弹簧在蹦着。

核心函数

其实这个弹簧功能是由一个函数来实现的。

public static Vector3 Lerp(Vector3 a, Vector3 b, float t);

解释一下这个函数:官网解释

通俗一点讲,第一个参数是我自己的位置,第二个参数是目标位置,第三个位置当成弹性系数就好。从我的位置弹回目标位置。

代码

//1. 左边受限,若往右移,则再弹回
        if (this.transform.localPosition.x > 0)
        {
            //Lerp方法用来做弹簧模拟
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, Vector3.zero, Time.deltaTime * K);
        }

        //2. 右边受限
        if (this.transform.localPosition.x < -3200)
        {
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-3200, 0, 0), Time.deltaTime * K);
        }

        //3. 整版卡位
        if(this.transform.localPosition.x < 0 && this.transform.localPosition.x >= -400)
        {
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, Vector3.zero, Time.deltaTime * K);
        }
        else if(this.transform.localPosition.x < -400 && this.transform.localPosition.x >= -800)
        {
            this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, new Vector3(-800,0,0), Time.deltaTime * K);
        }

整版卡位我只写了第一个面板,后面的面板函数都一样,把里面的参数改下就行了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值