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

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

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

相关公式:

ObliqueThrow.cs

 using System;
using UnityEngine; public class ObliqueThrow : MonoBehaviour { private float gravity; //重力加速度(当前)
private float gravityValue = -9.8f; //重力加速度值1
private Vector2 horizontalDir; //水平方向
private float startSpeed; //初速度
private float sinValue; //sin值
private float cosValue; //cos值
private Vector3 startPos; //开始位置
private float endY; //结束高度(地面高度)
private float timer; //运动消耗时间
private Action<GameObject> finishCallBack; //落地后的回调
private bool canMove = false; //能否运动 private float timeScale = ; //时间系数
private float reboundTimes = ; //反弹次数
private bool isInRebound = false; //是否在反弹中
private float lastX; //落地点x
private float lastZ; //落地点z
private float hightestPointTime; //到达最高点所用的时间
private float hightestPointStayTime = 0.1f; //最高点减速范围的时间段 void Update()
{
if (!canMove)
{
return;
} //移动过程
timer = timer + Time.deltaTime * timeScale; float xOffset = startSpeed * timer * cosValue * horizontalDir.x;
float zOffset = startSpeed * timer * cosValue * horizontalDir.y;
float yOffset = startSpeed * timer * sinValue + 0.5f * gravity * timer * timer; Vector3 endPos;
if (!isInRebound) //非反弹过程
{
endPos = startPos + new Vector3(xOffset, yOffset, zOffset);
if (Mathf.Abs(timer - hightestPointTime) < hightestPointStayTime)
{
timeScale = 0.5f;
}
else
{
timeScale = 1f;
}
}
else //反弹过程
{
endPos = new Vector3(lastX, yOffset, lastZ);
} //落地
if (endPos.y < endY)
{
endPos.y = endY; reboundTimes = reboundTimes - ;
if (reboundTimes < ) //移动结束
{
canMove = false;
}
else //落地反弹
{
gravity = gravityValue * ;
horizontalDir = Vector2.zero;
startSpeed = startSpeed * 0.8f;
sinValue = ;
cosValue = ;
timer = ; isInRebound = true;
lastX = endPos.x;
lastZ = endPos.z;
}
}
transform.position = endPos; //移动结束
if (!canMove)
{
finishCallBack(gameObject);
Destroy(this);
}
} public void StartMove(Vector2 horizontalDir, float startSpeed, float angle, float endY, Action<GameObject> finishCallBack)
{
gravity = gravityValue;
this.horizontalDir = horizontalDir;
this.startSpeed = startSpeed;
sinValue = Mathf.Sin(Mathf.Deg2Rad * angle);
cosValue = Mathf.Cos(Mathf.Deg2Rad * angle);
startPos = transform.position;
this.endY = endY;
timer = ;
this.finishCallBack = finishCallBack;
canMove = true; hightestPointTime = Mathf.Abs(startSpeed * sinValue / gravity);
}
}

TestThrow.cs

 using UnityEngine;
using System.Collections.Generic; public class TestThrow : MonoBehaviour { public GameObject testGo;
private bool isDebug = false;
private List<GameObject> drawGoList = new List<GameObject>(); void Update ()
{
if (Input.GetKeyDown(KeyCode.Q))
{
//半径为1的方向圆
float randomNum = Random.Range(0f, 1f);//[0, 1]
float x = randomNum * - ;//[-1, 1]
float z = Mathf.Sqrt( - x * x);
if (Random.Range(0f, 1f) > 0.5f)
{
z = -z;
} isDebug = true;
ObliqueThrow obliqueThrow = testGo.AddComponent<ObliqueThrow>();
obliqueThrow.StartMove(new Vector2(, ), 5f, 45f, 0f, (go) => {
isDebug = false;
Debug.Log("移动结束");
});
}
else if(Input.GetKeyDown(KeyCode.W))
{
testGo.transform.position = new Vector3(0f, 5f, 0f);
for (int i = ; i < drawGoList.Count; i++)
{
Destroy(drawGoList[i]);
}
drawGoList.Clear();
} if (isDebug)
{
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.transform.position = testGo.transform.position;
go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
drawGoList.Add(go);
}
}
}

效果如下:

[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. 基于STM32的红外遥控重点解析

    本文有两个内容:一.红外遥控协议的的讲解:二.解码程序解析(参考正点原子的代码) 红外的介绍.优点.缺点就不给大家说了,进入正题 一.红外遥控协议的的讲解 红外遥控的编码目前广泛使用的是:NEC Pr ...

  2. jquery.nicescroll.min.js滚动条使用方法

    jquery.nicescroll.min.js滚动条使用方法,Nicescroll 是制作自定义滚动条的jq插件.支持div,iframe,html等使用,兼容IE7-8,safari,firefo ...

  3. oracle sql 添加、修改数据库操作方式

    年龄大了,写程序总记不住.记录一下格式: private const string SQL_INSTERT = @"INSERT INTO QS_ROOM_QUEUE (QUEUEID,RO ...

  4. windows defender和windows firewall

    Windows defender: Windows Defender,曾用名Microsoft Anti Spyware,是一个杀毒程序,可以运行在Windows XP和Windows Server ...

  5. java容器Container和组件Component之GUI

    GUI全称Graphical User Interfaces,意为图形用户户界面,又称为图形用户接口,GUI指的就是采用图形方式显示的计算机操作用户界面,打个比方吧,我们点击QQ图标,就会弹出一个QQ ...

  6. vi 编辑器基本命令

    命令模式(esc) k 上移一行j 下移一行h 左移一行l 右移一行 6j  下移6行 5k 上移5行 0 将游标放在一行的开始$ 将游标放在一行的末尾w 将游标移动到下一个单词b 将游标移动到上一个 ...

  7. linux 系统下有sda和hda的硬件设备分别代表什么意思

    linux 系统下有sda和hda的硬件设备分别代表什么意思/dev/sda1 # SCSI设备,sda,sdb,sdc,三块盘,1,2,3代表分区(PV)/dev/sda2/dev/sdb1/dev ...

  8. 获取一个div下的li或者img元素

    上层div的xpath=//*[@id="launchpadOptionsList"] 其下的所有li的最后一个是//*[@id="launchpadOptionsLis ...

  9. 【springBoot】之定制Banner

    springboot启动时控制台打印图案如下: 1.假如我们不想看到这个图案 public static void main(String[] args) { SpringApplication ap ...

  10. SpringMVC---400错误The request sent by the client was syntactically incorrect ()

    在SpringMVC中使用@RequestBody和@ModelAttribute注解时遇到了很多问题,现记录下来. @ModelAttribute这个注解主要是将客户端请求的参数绑定参数到一个对象上 ...