• [Unity算法]平抛运动


    平抛运动:

    1.物体以一定的初速度水平方向抛出,如果物体仅受重力作用,这样的运动叫做平抛运动。

    2.平抛运动可看作水平方向的匀速直线运动以及竖直方向的自由落体运动的合运动。

    水平方向位移:s = v * t

    竖直方向位移:h = 1/2 * g * t * t

    3.平抛物体的运动轨迹为抛物线。

    HorizontalThrow.cs

     1 using System;
     2 using UnityEngine;
     3 
     4 public class HorizontalThrow : MonoBehaviour {
     5 
     6     private readonly float gravity = -9.8f;     //重力加速度
     7     private Vector2 horizontalDir;              //水平方向
     8     private float horizontalSpeed;              //水平速度
     9     private Vector3 startPos;                   //开始位置
    10     private float endY;                         //结束高度(地面高度)
    11     private float timer;                        //运动消耗时间
    12     private Action<GameObject> finishCallBack;  //落地后的回调
    13     private bool canMove = false;               //能否运动
    14 
    15     void Update ()
    16     {
    17         if (!canMove)
    18         {
    19             return;
    20         }
    21 
    22         //移动过程
    23         timer = timer + Time.deltaTime;
    24 
    25         float xOffset = horizontalSpeed * horizontalDir.x * timer;
    26         float zOffset = horizontalSpeed * horizontalDir.y * timer;
    27         float yOffset = 0.5f * gravity * timer * timer;
    28 
    29         Vector3 endPos = startPos + new Vector3(xOffset, yOffset, zOffset);
    30         if (endPos.y < endY)
    31         {
    32             endPos.y = endY;
    33             canMove = false;
    34         }
    35         transform.position = endPos;
    36 
    37         //移动结束
    38         if (!canMove)
    39         {
    40             finishCallBack(gameObject);
    41             Destroy(this);
    42         }
    43     }
    44 
    45     public void StartMove(Vector2 horizontalDir, float horizontalSpeed, float endY, Action<GameObject> finishCallBack)
    46     {
    47         this.horizontalDir = horizontalDir;
    48         this.horizontalSpeed = horizontalSpeed;
    49         startPos = transform.position;
    50         this.endY = endY;
    51         timer = 0;
    52         this.finishCallBack = finishCallBack;
    53         canMove = true;
    54     }
    55 }

    TestHorizontalThrow.cs

     1 using UnityEngine;
     2 using System.Collections.Generic;
     3 
     4 public class TestHorizontalThrow : 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 * 2 - 1;//[-1, 1]
    17             float z = Mathf.Sqrt(1 - x * x);
    18             if (Random.Range(0f, 1f) > 0.5f)
    19             {
    20                 z = -z;
    21             }
    22 
    23             isDebug = true;
    24             HorizontalThrow horizontalThrow = testGo.AddComponent<HorizontalThrow>();
    25             horizontalThrow.StartMove(new Vector2(x, z), 3f, 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 = 0; 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 }

    效果如下:

  • 相关阅读:
    win10 uwp 弹起键盘不隐藏界面元素
    win10 uwp 存放网络图片到本地
    win10 uwp 存放网络图片到本地
    sublime Text 正则替换
    sublime Text 正则替换
    win10 uwp 绘图 Line 控件使用
    win10 uwp 绘图 Line 控件使用
    AJAX 是什么?
    什么是 PHP SimpleXML?
    PHP XML DOM:DOM 是什么?
  • 原文地址:https://www.cnblogs.com/lyh916/p/10391632.html
Copyright © 2020-2023  润新知