斜抛运动:

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算法]斜抛运动的更多相关文章

  1. [Unity算法]斜抛运动(变种)

    之前的斜抛运动,如果运动到游戏中,显然是太呆板了,那么可以试着加入一些效果,让它看起来更生动一些,类似游戏中的击飞或者掉落效果: 1.在达到最高点的时间点±X的时间段内,会有“减速”效果,形成一种在空 ...

  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. MapReduce实现与自定义词典文件基于hanLP的中文分词详解

    前言: 文本分类任务的第1步,就是对语料进行分词.在单机模式下,可以选择python jieba分词,使用起来较方便.但是如果希望在Hadoop集群上通过mapreduce程序来进行分词,则hanLP ...

  2. Spring Boot 配置详解

    Spring Boot 针对常用的开发场景提供了一系列自动化配置来减少原本复杂而又几乎很少改动的模板配置内容,但是,我们还是需要了解如何在Spring Boot中修改这些自动化的配置,以应对一些特殊场 ...

  3. backgroud 应用减小资源大小和请求数

    一,一个典型的应用,利用小图的自动延伸,实现整个网页背景图,充分节约资源宽带.如:汽车之家的404页背景图就是这样 <div style="height: 3000px; backgr ...

  4. Docker-compose ports和expose的区别

    docker-compose中有两种方式可以暴露容器的端口:ports和expose. 1 ports ports暴露容器端口到主机的任意端口或指定端口,用法: ports: - "80:8 ...

  5. Winform 界面全屏 显示状态栏

    this.FormBorderStyle = FormBorderStyle.None; this.MaximumSize = new Size(Screen.PrimaryScreen.Workin ...

  6. WPF Demo18 路由事件

    using System.Windows; namespace 路由事件2 { public class Student { ////声明并定义路由事件 //public static readonl ...

  7. 同台同时多开DELPHI2007的解决办法

    Cannot create file "C:\Users\Administrator\AppData\Local\Temp\EditorLineEnds.ttr"这个问题的产生根据 ...

  8. Oracle 故障整理

    #created at 18-10-28 by nod #提示磁盘空间已满 不能登录数据库 18-10-28 [oracle@oracle ~]$ sqlplus / as sysdba SQL*Pl ...

  9. C语言强化——排序

    1.完成堆排,对比堆排和qsort在排序1亿数的时间差异 #include<stdio.h> #include<time.h> #include<stdlib.h> ...

  10. F5负载均衡原理(转载)

    https://blog.csdn.net/panxueji/article/details/42647193 一. 负载均衡技术 负载均衡技术在现有网络结构之上提供了一种廉价.有效.透明的方法,来扩 ...