之前的斜抛运动,如果运动到游戏中,显然是太呆板了,那么可以试着加入一些效果,让它看起来更生动一些,类似游戏中的击飞或者掉落效果:

1.在达到最高点的时间点±X的时间段内,会有“减速”效果,形成一种在空中停留的感觉

2.落地后,反弹一次,再落地,就像是与地面发生了碰撞

相关公式:

ObliqueThrow.cs

  1. using System;
  2. using UnityEngine;
  3.  
  4. public class ObliqueThrow : MonoBehaviour {
  5.  
  6. private float gravity; //重力加速度(当前)
  7. private float gravityValue = -9.8f; //重力加速度值1
  8. private Vector2 horizontalDir; //水平方向
  9. private float startSpeed; //初速度
  10. private float sinValue; //sin值
  11. private float cosValue; //cos值
  12. private Vector3 startPos; //开始位置
  13. private float endY; //结束高度(地面高度)
  14. private float timer; //运动消耗时间
  15. private Action<GameObject> finishCallBack; //落地后的回调
  16. private bool canMove = false; //能否运动
  17.  
  18. private float timeScale = ; //时间系数
  19. private float reboundTimes = ; //反弹次数
  20. private bool isInRebound = false; //是否在反弹中
  21. private float lastX; //落地点x
  22. private float lastZ; //落地点z
  23. private float hightestPointTime; //到达最高点所用的时间
  24. private float hightestPointStayTime = 0.1f; //最高点减速范围的时间段
  25.  
  26. void Update()
  27. {
  28. if (!canMove)
  29. {
  30. return;
  31. }
  32.  
  33. //移动过程
  34. timer = timer + Time.deltaTime * timeScale;
  35.  
  36. float xOffset = startSpeed * timer * cosValue * horizontalDir.x;
  37. float zOffset = startSpeed * timer * cosValue * horizontalDir.y;
  38. float yOffset = startSpeed * timer * sinValue + 0.5f * gravity * timer * timer;
  39.  
  40. Vector3 endPos;
  41. if (!isInRebound) //非反弹过程
  42. {
  43. endPos = startPos + new Vector3(xOffset, yOffset, zOffset);
  44. if (Mathf.Abs(timer - hightestPointTime) < hightestPointStayTime)
  45. {
  46. timeScale = 0.5f;
  47. }
  48. else
  49. {
  50. timeScale = 1f;
  51. }
  52. }
  53. else //反弹过程
  54. {
  55. endPos = new Vector3(lastX, yOffset, lastZ);
  56. }
  57.  
  58. //落地
  59. if (endPos.y < endY)
  60. {
  61. endPos.y = endY;
  62.  
  63. reboundTimes = reboundTimes - ;
  64. if (reboundTimes < ) //移动结束
  65. {
  66. canMove = false;
  67. }
  68. else //落地反弹
  69. {
  70. gravity = gravityValue * ;
  71. horizontalDir = Vector2.zero;
  72. startSpeed = startSpeed * 0.8f;
  73. sinValue = ;
  74. cosValue = ;
  75. timer = ;
  76.  
  77. isInRebound = true;
  78. lastX = endPos.x;
  79. lastZ = endPos.z;
  80. }
  81. }
  82. transform.position = endPos;
  83.  
  84. //移动结束
  85. if (!canMove)
  86. {
  87. finishCallBack(gameObject);
  88. Destroy(this);
  89. }
  90. }
  91.  
  92. public void StartMove(Vector2 horizontalDir, float startSpeed, float angle, float endY, Action<GameObject> finishCallBack)
  93. {
  94. gravity = gravityValue;
  95. this.horizontalDir = horizontalDir;
  96. this.startSpeed = startSpeed;
  97. sinValue = Mathf.Sin(Mathf.Deg2Rad * angle);
  98. cosValue = Mathf.Cos(Mathf.Deg2Rad * angle);
  99. startPos = transform.position;
  100. this.endY = endY;
  101. timer = ;
  102. this.finishCallBack = finishCallBack;
  103. canMove = true;
  104.  
  105. hightestPointTime = Mathf.Abs(startSpeed * sinValue / gravity);
  106. }
  107. }

TestThrow.cs

  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class TestThrow : MonoBehaviour {
  5.  
  6. public GameObject testGo;
  7. private bool isDebug = false;
  8. private List<GameObject> drawGoList = new List<GameObject>();
  9.  
  10. void Update ()
  11. {
  12. if (Input.GetKeyDown(KeyCode.Q))
  13. {
  14. //半径为1的方向圆
  15. float randomNum = Random.Range(0f, 1f);//[0, 1]
  16. float x = randomNum * - ;//[-1, 1]
  17. float z = Mathf.Sqrt( - x * x);
  18. if (Random.Range(0f, 1f) > 0.5f)
  19. {
  20. z = -z;
  21. }
  22.  
  23. isDebug = true;
  24. ObliqueThrow obliqueThrow = testGo.AddComponent<ObliqueThrow>();
  25. obliqueThrow.StartMove(new Vector2(, ), 5f, 45f, 0f, (go) => {
  26. isDebug = false;
  27. Debug.Log("移动结束");
  28. });
  29. }
  30. else if(Input.GetKeyDown(KeyCode.W))
  31. {
  32. testGo.transform.position = new Vector3(0f, 5f, 0f);
  33. for (int i = ; i < drawGoList.Count; i++)
  34. {
  35. Destroy(drawGoList[i]);
  36. }
  37. drawGoList.Clear();
  38. }
  39.  
  40. if (isDebug)
  41. {
  42. GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  43. go.transform.position = testGo.transform.position;
  44. go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
  45. drawGoList.Add(go);
  46. }
  47. }
  48. }

效果如下:

[Unity算法]斜抛运动(变种)的更多相关文章

  1. [Unity算法]斜抛运动

    斜抛运动: 1.物体以一定的初速度斜向射出去,物体所做的这类运动叫做斜抛运动. 2.斜抛运动看成是作水平方向的匀速直线运动和竖直方向的竖直上抛运动的合运动. 3.它的运动轨迹是抛物线. Oblique ...

  2. Javascript摸拟自由落体与上抛运动 说明!

    JavaScript 代码 //**************************************** //名称:Javascript摸拟自由落体与上抛运动! //作者:Gloot //邮箱 ...

  3. [Unity算法]平抛运动

    平抛运动: 1.物体以一定的初速度水平方向抛出,如果物体仅受重力作用,这样的运动叫做平抛运动. 2.平抛运动可看作水平方向的匀速直线运动以及竖直方向的自由落体运动的合运动. 水平方向位移:s = v ...

  4. canvas 模拟小球上抛运动的物理效果

    最近一直想用学的canvas做一个漂亮的小应用,但是,发现事情并不是想的那么简单.比如,游戏的逼真效果,需要自己来coding…… 所以,自己又先做了一个小demo,算是体验一下亲手打造物理引擎的感觉 ...

  5. [Unity算法]点是否在多边形范围内

    参考链接: https://www.zhihu.com/question/26551754 http://www.cnblogs.com/leoin2012/p/6425089.html 原理如下: ...

  6. [Unity算法]弧度和角度

    参考链接: https://zhidao.baidu.com/question/576596182.html 1.弧度和角度的转换 2.sin函数 3.cos函数 4.tan函数 5.特殊的三角函数值 ...

  7. [Unity算法]A星寻路(一):基础版本

    参考链接: https://www.cnblogs.com/yangyxd/articles/5447889.html 一.原理 1.将场景简化,分割为一个个正方形格子,这些格子称之为节点(node) ...

  8. as3.0两点之间简单的运动,斜着运动,任意两点

    import flash.utils.Timer;import flash.events.TimerEvent;//fixed结束点//sprite初始点var fixedX:Number = fix ...

  9. 【HDU 4445】Crazy Tank(暴力)

    高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include &qu ...

随机推荐

  1. LB+nginx+tomcat7集群模式下的https请求重定向(redirect)后变成http的解决方案

    0. 环境信息 Linux:Linux i-8emt1zr1 2.6.32-573.el6.x86_64 #1 SMP Wed Jul 1 18:23:37 EDT 2015 x86_64 x86_6 ...

  2. OpenWrt实现802.11s组网模式

    参考 http://www.docin.com/p-277067204.html 无线网卡wlan0正常后,输入一下命令 iw dev wlan0 interface add mesh_iface t ...

  3. jQuery自定义扩展写法

    jQuery自定义扩展有两种写法: $.extend({}) ;           调用方法:      $.方法          $.fn.extend({});        调用方法:    ...

  4. IKAnalyzer 添加扩展词库和自定义词

    原文链接http://blog.csdn.net/whzhaochao/article/details/50130605 IKanalyzer分词器 IK分词器源码位置 http://git.osch ...

  5. 简单明了区分IE,Firefox,chrome主流浏览器

    简单明了判断浏览器Firefox:typeof navigator !== 'undefined' && navigator.userAgent.toLowerCase().index ...

  6. 解决读取Excel表格中某列数据为空的问题 c#

    解决同一列中“字符串”和“数字”两种格式同时存在,读取时,不能正确显示“字符串”格式的问题:set xlsconn=CreateObject("ADODB.Connection") ...

  7. 【Maven】从Maven中导出项目依赖的Jar包

    从SVN上下载源代码 svn export https://10.200.1.201/xxxx/PLATFORM code/ --force --username xxx --password xxx ...

  8. HTTP请求报文

    一个HTTP请求报文,有请求行request line.请求头部header.空行和请求数据组成.看下图可知: 1.请求行 请求行:请求方法.请求地址和协议版本. 请求方法 HTTP/1.1 定义的请 ...

  9. 【模态窗口-Modeldialog】提交请求时禁止在新窗口打开页面的处理方法

    在使用Window.ShowModalDialog()打开模态窗口后,在模态窗口内提交时总是会在新窗口中打开. 解决办法: 在要弹出的窗口的<head>之间加: <base targ ...

  10. 服务容错保护断路器Hystrix之二:Hystrix工作流程解析

    一.总运行流程 当你发出请求后,hystrix是这么运行的 红圈 :Hystrix 命令执行失败,执行回退逻辑.也就是大家经常在文章中看到的“服务降级”. 绿圈 :四种情况会触发失败回退逻辑( fal ...