Common(九)—— BeatFlyMotion

目录为:Assets/Scripts/Common/,
BeatFlyMotion.cs
这个文件主要实现一个向上击飞的功能.
看一下效果:

这里写图片描述

把脚本挂到一个cube上即可.
下面是代码:

using UnityEngine;
using System.Collections.Generic;

//击飞运动效果
public class BeatFlyMotion: MonoBehaviour
{
    //当前影响的buff的id
    public uint mBuffId = 0;

    //默认高度
    private float mDefaultHeight;

    //向上初始速度
    private float mUpSpeed = 0;

    //向上加速度
    private float mUpAcceleration;

    //向下初始速度
    private float mDownSpeed = 0;

    //向下加速度
    private float mDownAcceleration;

    //滞空时间
    public float mStayTime;

    //当前高度
    public float mCurrentHeight;

    //上次速度
    private float mLastSpeed;

    //当前速度
    private float mCurrentSpeed;

    //激活运动
    private bool mEnable = false;

    //滞空持续时间
    private float mTotalStayTime = 0;

    //运动方向
    //控制是否是向上运动
    private bool bDir = true;

    //击飞是否能被覆盖
    private int mCanBeRecover = 1;

    private Transform mTransform = null;
    private Animation mAnimation = null;


    void Start()
    {

    }

    void OnEnable()
    {
        //重置数据
        mTotalStayTime = 0;
        mEnable = false;
        bDir = true;
        mCanBeRecover = 1;
        mBuffId = 0;
    }

    private Animation GetAnimation()
    {
        if (mAnimation == null)
        {
            mAnimation = gameObject.GetComponent<Animation> ();
        }

        return mAnimation;
    }

    //击飞
    public void BeatFly(uint buffId, string action, float upSpeed = 5, float upAcceleration = -5, float downSpeed = 5, float downAcceleration = 15, float stayTime = 0, int canRecover = 1)
    {
        //已经是击飞状态
        if (mEnable)
        {
            //Debug.Log("recover state" + mCanBeRecover.ToString());
            //已经是击飞状态下,再执行BeatFly时的判断
            //可以在空中进行连续的击飞状态
            if (mCanBeRecover == 1)
            {
                Reset ();
            }
            else
            {
                return;
            }
        }

        mBuffId = buffId;

        //击飞是否被覆盖
        mCanBeRecover = canRecover;

        //默认高度
        mDefaultHeight = gameObject.transform.position.y;
        //当前高度
        mCurrentHeight = mDefaultHeight;
        //mTransform
        mTransform = gameObject.transform;

        //downAcceleration默认等于5
        if (downAcceleration < 0)
        {
            Debug.LogError ("downAcceleration do not set negative in BeatFly");
        }

        if (stayTime == 0)
        {
            stayTime = 0.0001f;
        }

        mUpSpeed = upSpeed;
        mUpAcceleration = upAcceleration;
        mDownSpeed = downSpeed;
        mDownAcceleration = downAcceleration;

        mStayTime = stayTime;
        mEnable = true;

        //初始速度
        mLastSpeed = mUpSpeed;
        //当前速度
        mCurrentSpeed = mUpSpeed;

        //执行被击飞的动作
        /*
        if (action != "0")
        {
            GetAnimation ().Play (action);
        }
        */
    }

    //立即下落
    public void FallNow()
    {
        mStayTime = 0.0001f;
    }

    void Update()
    {
        //击飞运动更新
        if (mEnable)
        {
            //进入滞空状态
            //允许滞空mStayTime时间,这个滞空时间不包括下落的时间
            //mCurrentSpeed的初始值为mUpSpeed
            //当 mCurrentSpeed <= 0 时就是向下运动了
            if (mCurrentSpeed <= 0 && mTotalStayTime < mStayTime)
            {
                mLastSpeed = -mDownSpeed;
                mCurrentSpeed = -mDownSpeed;

                mTotalStayTime += Time.deltaTime;
                bDir = false;
                return;
            }

            //当前速度v = vo + at

            //向上运动
            if (bDir)
            {
                mCurrentSpeed = mCurrentSpeed + mUpAcceleration * Time.deltaTime;
            }
            //向下运动
            else
            {
                //默认的mDownAcceleration要比向上加速度大一些
                mCurrentSpeed = mCurrentSpeed - mDownAcceleration * Time.deltaTime;
            }

            //运动距离
            //匀加速直线运动的位移公式(化简过的)
            float dist = (mLastSpeed + mCurrentSpeed) * Time.deltaTime * 0.5f;

            //当前高度
            mCurrentHeight += dist;

            //保存前一次的速度
            mLastSpeed = mCurrentSpeed;

            mTransform.position = new Vector3 (mTransform.position.x, mCurrentHeight, mTransform.position.z);
            //回到原来位置或者反向速度为0
            if (mCurrentHeight < mDefaultHeight)
            {
                //Reset后,mEnable为false, 运动也就停了
                Reset ();
            }
        }
    }

    void Reset()
    {
        mCurrentHeight = mDefaultHeight;

        //重置数据
        mTotalStayTime = 0;
        mEnable = false;
        bDir = true;
        mCanBeRecover = 1;
        mBuffId = 0;

        mTransform.localPosition = new Vector3 (mTransform.localPosition.x, mDefaultHeight, mTransform.localPosition.z);
    }

    //点击按钮
    public void ClickButton()
    {
        BeatFly (1, "aaa", 5, -5, 5, 5, 0.05f, 1);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值