[Unity算法]斜抛运动
斜抛运动:
1.物体以一定的初速度斜向射出去,物体所做的这类运动叫做斜抛运动。
2.斜抛运动看成是作水平方向的匀速直线运动和竖直方向的竖直上抛运动的合运动。

3.它的运动轨迹是抛物线。

ObliqueThrow.cs
using System;
using UnityEngine; public class ObliqueThrow : MonoBehaviour { private readonly float gravity = -9.8f; //重力加速度
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; //能否运动 void Update()
{
if (!canMove)
{
return;
} //移动过程
timer = timer + Time.deltaTime; 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 = startPos + new Vector3(xOffset, yOffset, zOffset);
if (endPos.y < endY)
{
endPos.y = endY;
canMove = false;
}
transform.position = endPos; //移动结束
if (!canMove)
{
finishCallBack(gameObject);
Destroy(this);
}
} public void StartMove(Vector2 horizontalDir, float startSpeed, float angle, float endY, Action<GameObject> finishCallBack)
{
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;
}
}
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算法]斜抛运动的更多相关文章
- [Unity算法]斜抛运动(变种)
之前的斜抛运动,如果运动到游戏中,显然是太呆板了,那么可以试着加入一些效果,让它看起来更生动一些,类似游戏中的击飞或者掉落效果: 1.在达到最高点的时间点±X的时间段内,会有“减速”效果,形成一种在空 ...
- Javascript摸拟自由落体与上抛运动 说明!
JavaScript 代码 //**************************************** //名称:Javascript摸拟自由落体与上抛运动! //作者:Gloot //邮箱 ...
- [Unity算法]平抛运动
平抛运动: 1.物体以一定的初速度水平方向抛出,如果物体仅受重力作用,这样的运动叫做平抛运动. 2.平抛运动可看作水平方向的匀速直线运动以及竖直方向的自由落体运动的合运动. 水平方向位移:s = v ...
- canvas 模拟小球上抛运动的物理效果
最近一直想用学的canvas做一个漂亮的小应用,但是,发现事情并不是想的那么简单.比如,游戏的逼真效果,需要自己来coding…… 所以,自己又先做了一个小demo,算是体验一下亲手打造物理引擎的感觉 ...
- [Unity算法]点是否在多边形范围内
参考链接: https://www.zhihu.com/question/26551754 http://www.cnblogs.com/leoin2012/p/6425089.html 原理如下: ...
- [Unity算法]弧度和角度
参考链接: https://zhidao.baidu.com/question/576596182.html 1.弧度和角度的转换 2.sin函数 3.cos函数 4.tan函数 5.特殊的三角函数值 ...
- [Unity算法]A星寻路(一):基础版本
参考链接: https://www.cnblogs.com/yangyxd/articles/5447889.html 一.原理 1.将场景简化,分割为一个个正方形格子,这些格子称之为节点(node) ...
- as3.0两点之间简单的运动,斜着运动,任意两点
import flash.utils.Timer;import flash.events.TimerEvent;//fixed结束点//sprite初始点var fixedX:Number = fix ...
- 【HDU 4445】Crazy Tank(暴力)
高中物理斜抛运动,简单分析一下角度固定下来则可以计算每个cannonball的降落坐标lnd. 因此暴力计算不同角度下的结果. #include <cstdio> #include &qu ...
随机推荐
- 安装plsql developer
需求:要连接oracle数据库,不想在本地安装oracle,太大,又占内存,所以用plsql developer.. 在网上看了很多博客,妈呀,被毒的不清,一直提示初始化失败,就是那个oci,dll ...
- Nuke中新建线程的方法
最近维护合成部门的nuke工具包,发现不少工具的使用方法都很个人化,没有说明文档.这也导致artist在使用工具的时候比较感性,调整参数的时候缺少前后逻辑,长此以往,artist会产生这种意识:只要最 ...
- workerman 简单笔记
运行命令 curl -Ss http://www.workerman.net/check.php | php 都显示的ok (注意:检测脚本中没有检测event扩展或者libevent扩展) ...
- DS二叉树--二叉树之数组存储
二叉树可以采用数组的方法进行存储,把数组中的数据依次自上而下,自左至右存储到二叉树结点中,一般二叉树与完全二叉树对比,比完全二叉树缺少的结点就在数组中用0来表示.,如下图所示 从上图可以看出,右边的是 ...
- 无后缀名伪静态路径在IIS7.0的网站提示 "404 - File or directory not found"
新配置服务器(windows server 2008,not sp1) 经测试情况如下: ①无后缀名伪静态路径行在IIS7.0的网站提示 ”404 - File or directory not fo ...
- 解决下载经过GZip压缩后的网页乱码问题
目前很多网站默认采用GZip压缩,如果不进行解压缩,下载后生成的html页面打开后会出现中文乱码 乱码前: string url = "http://quote.eastmoney.com/ ...
- 【Mysql】事务日志-Write Ahead logging vs command-logging(转)
原理讲解: Write Ahead logging vs command logging Write Ahead logging 持久化数据保存在磁盘,数据的存储是随机的,并非顺序: 内存中保存磁盘数 ...
- 查看php-fpm开启的进程数以及每个进程的内存限制
查看php-fpm开启的进程数以及每个进程的内存限制 1.通过命令查看服务器上一共开了多少的 php-cgi 进程 ps -fe |grep "php-fpm"|grep &qu ...
- mac nginx 一些资料
http://www.jianshu.com/p/918eb337a206 mac 的nginx 配置目录在/usr/local/etc/nginx 安装之前最好执行brew的update和upgra ...
- Java-Runoob-高级教程-实例-数组:13. Java 实例 – 数组交集
ylbtech-Java-Runoob-高级教程-实例-数组:13. Java 实例 – 数组交集 1.返回顶部 1. Java 实例 - 数组交集 Java 实例 以下实例演示了如何使用 reta ...