​之前一直都是自己学习Unity各种做Demo,最近开始正式使用Unity来做一个款2d的游戏。

其中在做一个类似小球弹跳运动的时候遇到了点问题,查找了很多资料,无意间发现AnimationCurve,顿时那种心情啊!

然后苦心钻研了一翻 抛砖引玉 的写了个Move2D的类主要是个大家一个思路。

不多说上正菜:

直线平移运动效果:

曲线上升运动效果:

曲线上升然后下降的弧线运动效果:

小球弹跳运动效果:

下面是C#代码,由于之前一直用Cocos2d-x所以有点cocos的风格:

using UnityEngine;
using System.Collections; public class Move2D : MonoBehaviour
{
public delegate void OverMoveEventHandler(GameObject gObj);
public event OverMoveEventHandler OverMove; public delegate void RunTimeEventHandler(GameObject gObj,RunTimeEventArgs bte);
public event RunTimeEventHandler RunTime;
public class RunTimeEventArgs : System.EventArgs
{
public readonly float runTime;
public readonly float totalTime; public RunTimeEventArgs (float rt,float tt)
{
runTime = rt;
totalTime = tt;
}
} private float moveTime = ;
private Vector3 speed;
private float timeDelta = ; private AnimationCurve anmc = null; private bool isMoveRuning = false;
public bool IsMoveRuning
{
get{return isMoveRuning;}
} private Vector2 moveEndPosition;
public Vector3 MoveEndPosition
{
get{return moveEndPosition;}
} private static Move2D createMove(GameObject runObj)
{
Move2D move2d = runObj.GetComponent<Move2D>(); if(move2d == null){
move2d = runObj.AddComponent<Move2D>();
} else {
//可以在这地方做标志达到动作序列,动作融合等等
} return move2d;
} /// <summary>
/// 创建一个轨迹是直线的运动
/// </summary>
/// <returns>返回 Move2D 运动实例</returns>
/// <param name="runObj">执行运动的物体</param>
/// <param name="endPostion">运动的终点</param>
/// <param name="moveTime">运动的时间</param>
public static Move2D createMove_Line(GameObject runObj,Vector3 endPostion,float moveTime)
{
Move2D move2d = createMove(runObj);
move2d.initMove_Line(endPostion,moveTime); return move2d;
} private void initMove_Line (Vector3 endPostion,float moveTime)
{
//直线运动不需要曲线进行y方向位移
anmc = null; moveEndPosition = endPostion;
speed = endPostion / moveTime;
timeDelta = ;
this.moveTime = moveTime;
isMoveRuning = true;
} /// <summary>
/// 创建一个轨迹是曲线上升的运动
/// </summary>
/// <returns>返回 Move2D 运动实例</returns>
/// <param name="runObj">执行运动的物体</param>
/// <param name="endPostion">运动的终点</param>
/// <param name="moveTime">运动的时间</param>
/// <param name="maxHeight">曲线的最大高度,在这个运动中指的是物体最终停留的高度</param>
/// <param name="curveCoefficient">曲线弧度系数.默认有一个系数计算方法,但是我只测试了高度为1-10的情况.如果运动曲线不是你期望的那么传入你需要的参数</param>
public static Move2D createMove_CurveUp(GameObject runObj,Vector3 endPostion,float moveTime,float maxHeight,float curveCoefficient = -999999f)
{
Move2D move2d = createMove(runObj); //测试范围 1-10 都没有问题,如果有问题的话自己传入curveCoefficient
if(curveCoefficient == -999999f){
curveCoefficient = maxHeight * 0.5f;
} move2d.initMove_CurveUp(endPostion,moveTime,maxHeight,curveCoefficient); return move2d;
} private void initMove_CurveUp (Vector3 endPostion,float moveTime,float maxHeight,float curveCoefficient)
{
//以下注释内容引用自 风宇冲的博客
//http://blog.sina.com.cn/s/blog_471132920101f8nv.html
//脚本创建AnimationCurve
//AnimationCurve可以理解为2部分 (1)键序列 (2)左右循环模式(又作左右包裹模式)
//一:键序列
//创建键序列:Keyframe[] ks = new Keyframe[3];
//曲线中加入键序列:AnimationCurve curve = new AnimationCurve(ks);
//获取曲线中的键序列:curve[index] 或者 curve.keys[index]
//添加单键:curve.Addkey(time,value)
//删除单键:curve.RemoveKey(index)
//二:左右循环
//anim.preWrapMode = WrapMode.Loop;
//anim.postWrapMode = WrapMode.Once;
//三:键
//Keyframe kf = new Keyframe(time,value);
//kf.inTangent = 45;
//kf.outTangent = 45; Keyframe[] kfs = new Keyframe[]; kfs[] = new Keyframe(,);
kfs[].outTangent = curveCoefficient; kfs[] = new Keyframe(moveTime,maxHeight); anmc = new AnimationCurve(kfs); moveEndPosition = endPostion;
speed = endPostion / moveTime;
timeDelta = ;
this.moveTime = moveTime;
isMoveRuning = true;
} /// <summary>
/// 创建一个轨迹是曲线上升到最大高度后下降的运动,当终点等于起点的时候就是原地跳跃
/// </summary>
/// <returns>返回 Move2D 运动实例</returns>
/// <param name="runObj">执行运动的物体</param>
/// <param name="endPostion">运动的终点</param>
/// <param name="moveTime">运动的时间</param>
/// <param name="maxHeight">曲线的最大高度,在这个运动中指的是物体运动轨迹的最大高度</param>
/// <param name="curveCoefficient">曲线弧度系数.默认有一个系数计算方法,但是我只测试了高度为1-10的情况.如果运动曲线不是你期望的那么传入你需要的参数</param>
public static Move2D createMove_CurveUpDown(GameObject runObj,Vector3 endPostion,float moveTime,float maxHeight,float curveCoefficient = -999999f)
{
Move2D move2d = createMove(runObj); //测试范围 1-10 都没有问题,如果有问题的话自己传入curveCoefficient
if(curveCoefficient == -999999f){
if(maxHeight >= )curveCoefficient = ((maxHeight-3f) * 0.5f) + 2.5f;
else {
curveCoefficient = maxHeight * 0.5f;
}
} move2d.initMove_CurveUpDown(endPostion,moveTime,maxHeight,curveCoefficient); return move2d;
} private void initMove_CurveUpDown (Vector3 endPostion,float moveTime,float maxHeight,float curveCoefficient)
{
Keyframe[] kfs = new Keyframe[]; kfs[] = new Keyframe(,);
kfs[].outTangent = curveCoefficient; kfs[] = new Keyframe(moveTime/,maxHeight);
kfs[].inTangent = 0f;
kfs[].outTangent = 0f; kfs[] = new Keyframe(moveTime,);
kfs[].inTangent = -curveCoefficient; anmc = new AnimationCurve(kfs); moveEndPosition = endPostion;
speed = endPostion / moveTime;
timeDelta = ;
this.moveTime = moveTime;
isMoveRuning = true;
} /// <summary>
/// 创建一个轨迹是落地弹跳小球的运动
/// </summary>
/// <returns>返回 Move2D 运动实例</returns>
/// <param name="runObj">执行运动的物体</param>
/// <param name="endPostion">运动的终点</param>
/// <param name="moveTime">运动的时间</param>
/// <param name="maxHeight">曲线的最大高度,在这个运动中指的是物体运动轨迹的第一个波峰高度,第二个波峰高度是最大高度的1/3,第三个波峰最大高度是第一个的1/5</param>
/// /// <param name="curveCoefficient">曲线弧度系数.默认有一个系数计算方法,但是我只测试了高度为1-10的情况.如果运动曲线不是你期望的那么传入你需要的参数</param>
public static Move2D createMove_Bounce3(GameObject runObj,Vector3 endPostion,float moveTime,float maxHeight,float curveCoefficient = -999999f)
{
Move2D move2d = createMove(runObj); //测试范围 1-10 都没有问题,如果有问题的话自己传入curveCoefficient
if(curveCoefficient == -999999f){
curveCoefficient = maxHeight;
} move2d.initMove_Bounce3(endPostion,moveTime,maxHeight,curveCoefficient); return move2d;
} private void initMove_Bounce3 (Vector3 endPostion,float moveTime,float maxHeight,float curveCoefficient)
{
Keyframe[] kfs = new Keyframe[]; kfs[] = new Keyframe(,);
kfs[].outTangent = curveCoefficient; kfs[] = new Keyframe(moveTime*0.35f,maxHeight);
kfs[].inTangent = ;
kfs[].outTangent = ; kfs[] = new Keyframe(moveTime*0.7f,);
kfs[].inTangent = -curveCoefficient;
kfs[].outTangent = curveCoefficient; kfs[] = new Keyframe(moveTime*0.8f,maxHeight/);
kfs[].inTangent = ;
kfs[].outTangent = ; kfs[] = new Keyframe(moveTime*0.9f,);
kfs[].inTangent = -curveCoefficient;
kfs[].outTangent = curveCoefficient; kfs[] = new Keyframe(moveTime*0.95f,maxHeight/);
kfs[].inTangent = ;
kfs[].outTangent = ; kfs[] = new Keyframe(moveTime,);
kfs[].inTangent = -curveCoefficient; anmc = new AnimationCurve(kfs); moveEndPosition = endPostion;
speed = endPostion / moveTime;
timeDelta = ;
this.moveTime = moveTime;
isMoveRuning = true;
} void Update () {
if(timeDelta <= moveTime){
Vector3 ep = speed * timeDelta; if(anmc != null){
ep.y += anmc.Evaluate(timeDelta);
} transform.localPosition = ep; if(RunTime != null)RunTime(gameObject,new RunTimeEventArgs(timeDelta,moveTime)); timeDelta += (Time.deltaTime);
} else {
if(RunTime != null)RunTime(gameObject,new RunTimeEventArgs(timeDelta,moveTime));
if(OverMove != null)OverMove(gameObject); StopMove2D();
}
} public void StopMove2D()
{
Destroy(this);
isMoveRuning = false;
} }

使用的时候只需要create你需要的运动就好:

OverMove事件在运动结束的时候发生;

RunTime事件在运动的每一帧发生,runTime是运动进行了多长时间,totalTime是传进去的那个运动总时间;

两个事件都会把gameObject传过去,可以利用它做一些处理;

void Start () {

//        Move2D.createMove_Line(gameObject,new Vector3(10,5,0),5f);
// Move2D.createMove_CurveUp(gameObject,new Vector3(10,2,0),5f,2f);
// Move2D.createMove_CurveUpDown(gameObject,new Vector3(10,3,0),5f,3f);
Move2D mv2d = Move2D.createMove_Bounce3(gameObject,new Vector3(,,),5f,4f);
mv2d.OverMove += new Move2D.OverMoveEventHandler(overMove);
// mv2d.RunTime += new Move2D.RunTimeEventHandler(moveTime);
} private void overMove(GameObject gObj)
{
Debug.Log("结束移动");
} private void moveTime(GameObject gObj,Move2D.RunTimeEventArgs rte)
{
Debug.Log("还有"+(rte.totalTime-rte.runTime)+"秒结束");
}

没有什么难点主要是对AnimationCurve的运用

我只是 抛砖引玉 具体的运动效果是要你项目需求来做的。

博客园不知道怎么上传附件,如果需要看Demo的话去这里​的附件下载把,如果有后面有更新我也会放在这里的。

最后感谢风宇冲的博客提供的帮助,如果有具体不知道怎么操作的小伙伴可以去看这个博客,里面详细的介绍了怎么用AnimationCurve。

欢迎转载收藏,转载著名出处!

Unity利用AnimationCurve做物体的各种运动的更多相关文章

  1. Unity 利用NGUI做屏幕分辨率适配+学习UIDraggablePanel的使用

    原地址:http://blog.sina.com.cn/s/blog_697b1b8c0101g2r4.html 大家使用unity,一定有看中其跨平台的强大功能,因此也难免会遇到不同屏幕分辨率适配的 ...

  2. Unity 利用NGUI2.6.3做技能冷却的CD效果

    NGUI非常强大我们今天来学习一下,如何利用NGUI做技能冷却的CD效果.先导入NGUI的插件.没有的话这里有啊NGUI2.6.3下载地址: http://vdisk.weibo.com/s/KLqn ...

  3. js 运动函数篇 (一) (匀速运动、缓冲运动、多物体运动、多物体不同值运动、多物体多值运动)层层深入

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS写 匀速运动.缓冲运动.多物体运 ...

  4. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之网格Meshes 本篇分享一下第5个已完工的视频,即<beginner Graphics – ...

  5. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials 既上一篇分享了中文字幕的灯光介绍Lights后,本篇分享一下第3个已完工 ...

  6. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之灯光介绍Lights 既上一篇分享了中文字幕的摄像机介绍Cameras后,本篇分享一下第2个已完工的 ...

  7. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras

    [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras 最近得到一些Unity官方视频教程,一看全是纯英文的讲解,没有任何字幕或者 ...

  8. JavaScript 运动(缓冲运动,多物体运动 ,多物体多值运动+回调机制)

    匀速运动   (当需要物体做匀速运动直接调用statMove函数) function startMove(dom,targetPosetion){ //dom : 运动对象,targetPositio ...

  9. 原生JavaScript运动功能系列(三):多物体多值运动

    多物体同时出发运动函数实现 多属性同步运动变化实现 一.多物同时触发运动函数实现 前面两个动画示例基本理解了动画的核心:位置变化和速度变化,操作的核心就是定时器分段叠加属性值.但是动画还是基于单个元素 ...

随机推荐

  1. kuangbin专题十二 HDU1114 Piggy-Bank (完全背包)

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. Qt 学习之路 2(15):标准对话框 QMessageBox

    Qt 学习之路 2(15):标准对话框 QMessageBox  豆子  2012年9月18日  Qt 学习之路 2  40条评论 所谓标准对话框,是 Qt 内置的一系列对话框,用于简化开发.事实上, ...

  3. Java快速IO(ACM)必备

    en.... 无非用到的是 1. new Scanner(System.in); 2.new BUfferReader(new InputStreamReader(System.in); 3.Syst ...

  4. Codeforces - 625B 贪心

    求最小不重复匹配次数 改最后一个字符最划算 我当时怎么就没看出来.. #include<bits/stdc++.h> using namespace std; string S,T; in ...

  5. 10-排序6 Sort with Swap(0, i) (25 分)

    Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order ...

  6. NPOI2.0导出excel之添加样式、边框和表头

    //优化后导出excel public System.IO.Stream ExcelStream(string search) // { var orderBusiniss = Containers. ...

  7. spring bean name生成规则

    现象: PVService PVServiceImpl ===>名称就是PVServiceImpl, 首字母没有小写 PageViewServiceImpl ==>名称是pageViewS ...

  8. Windows下 virtualenv 使用

    Windows下 virtualenv 使用 win python virtualenv 首先在电脑上安装两个不同版本的python mkvirtualenv --python C:\Python34 ...

  9. php数组·的方法1-数组统计函数

    /** * 下面是数组统计函数 * * * **/ //count() 数组的长度 print_r(count($arr3)); echo '<hr>'; //max() min() 数组 ...

  10. Subarray Sum K

    Given an nonnegative integer array, find a subarray where the sum of numbers is k. Your code should ...