Unity功能 实现左右加速度

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LightDigital.UI;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.EventSystems;
using LightDigital.Core;

public class C0001 : QuestionBase
{
    enum Dir {
        Dir_None,
        Dir_Left,
        Dir_Right,
        Dir_Over
    }
    public List<GameObject> listBox = new List<GameObject>();
    Queue<GameObject> queBox = new Queue<GameObject>();
    Button btnMoney;
    Image imgPersonStatic;
    Image imgPersonLeft;
    Image imgPersonRight;
    int fPersonLeftStartX = 0;
    int fPersonLeftEndX = -750;
    int fPersonRightStartX = -750;
    int fPersonRightEndX = 0;

    float fSpeed = 1;//速度
    float fAddSpeed = 5f;//每次增加的速度
    Dir eDir = Dir.Dir_None;//当前方向  
    

    void Awake()
    {                
        btnMoney = UIHelper.GetButton(gameObject, "btnMoney");
        imgPersonStatic = UIHelper.GetImage(gameObject, "imgPersonStatic");
        imgPersonLeft = UIHelper.GetImage(gameObject, "imgPersonLeft");
        imgPersonRight = UIHelper.GetImage(gameObject, "imgPersonRight");
    }
   
    void Start()
    {
        btnMoney.onClick.AddListener(onBtnCallback);
        imgPersonLeft.gameObject.SetActive(false);
        imgPersonRight.gameObject.SetActive(false);        
    }
    
    void Update()
    {
        if (!imgPersonStatic.gameObject.activeSelf)
        {
            switch (eDir)
            {
                case Dir.Dir_Left:     
                    imgPersonLeft.gameObject.SetActive(true);
                    imgPersonLeft.transform.Translate(Vector3.left * fSpeed * Time.deltaTime);//加速度
                    
                    if (imgPersonLeft.GetComponent<RectTransform>().anchoredPosition.x < fPersonLeftEndX)
                    {
                        eDir = Dir.Dir_Right;
                        imgPersonLeft.gameObject.SetActive(false);
                        imgPersonLeft.GetComponent<RectTransform>().anchoredPosition = v2Left;                          
                    }
                    break;
                case Dir.Dir_Right:
                    imgPersonRight.gameObject.SetActive(true);
                    imgPersonRight.transform.Translate(Vector3.right * fSpeed * Time.deltaTime);//加速度
                    if (imgPersonRight.GetComponent<RectTransform>().anchoredPosition.x > fPersonRightEndX)
                    {
                        eDir = Dir.Dir_Left;
                        imgPersonRight.GetComponent<RectTransform>().anchoredPosition = v2Right;
                        imgPersonRight.gameObject.SetActive(false);                        
                    }
                    break;
            }
        }
    }   

    void onBtnCallback()
    {
        imgPersonStatic.gameObject.SetActive(false);
        fSpeed *= fAddSpeed;
        switch (eDir)
        {
            case Dir.Dir_None:
                eDir = Dir.Dir_Left;                
                break;
            case Dir.Dir_Over:
                break;
        }
    }       

   
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值