又快两个月没写文章了,,最近一直忙的~~
今天来实现个抛物线的demo,可控制角度、力度、飞行速度、阻力、重力等等,基本上把能包含的属性都包含了。
本来是想把预飞行轨迹分享出来的,但实在是没时间总结。。。就先分享下抛物线效果吧!!
上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ParabolaTest : MonoBehaviour { public float angle = 60; // 角度 public float power = 15; // 力度 public float flySpeed = 2; // 飞行速度 public float resistance = -5; // 阻力 public float gravity = -9.81f; // 重力 private Vector3 startSpeed; // 初速度向量 private Vector3 gravitySpeed = Vector3.zero; // 重力速度向量 private float flyTimer; // 飞行时间 private Vector3 prevPosition; // Use this for initialization void Start () { startSpeed = Quaternion.Euler(new Vector3(-angle, 0, 0)) * Vector3.forward * power; } void FixedUpdate () { float flySpeed = Time.fixedDeltaTime * this.flySpeed; float resistance = Time.fixedDeltaTime * this.resistance; gravitySpeed.y = gravity * (flyTimer += flySpeed); if (startSpeed.y + gravitySpeed.y < 0) { //gravitySpeed.z += resistance; } gravitySpeed.z += resistance; transform.position += (startSpeed + gravitySpeed) * flySpeed; if (prevPosition != Vector3.zero && transform.position != prevPosition) { transform.rotation = Quaternion.FromToRotation(-Vector3.forward, transform.position - prevPosition); //旋转箭头,指向下一个移动的坐标点 } prevPosition = transform.position; } } |
Demo下载地址:
https://github.com/654306663/ParabolaTest
- 本文固定链接: http://www.u3d8.com/?p=1921
- 转载请注明: 网虫虫 在 u3d8.com 发表过