public float Power = 10;//这个代表发射时的速度/力度等,可以通过此来模拟不同的力大小
public float Angle = 45;//发射的角度,这个就不用解释了吧
public float Gravity = -10;//这个代表重力加速度
private Vector3 MoveSpeed;//初速度向量
private Vector3 GritySpeed = Vector3.zero;//重力的速度向量,t时为0
private float dTime;//已经过去的时间
private Vector3 currentAngle;
void Start()
{
//通过一个公式计算出初速度向量
//角度*力度
MoveSpeed = Quaternion.Euler(new Vector3(0, 0, Angle)) * Vector3.right * Power;
currentAngle = Vector3.zero;
}
void FixedUpdate()
{
//计算物体的重力速度
//v = at ;
GritySpeed.y = Gravity * (dTime += Time.fixedDeltaTime);
//位移模拟轨迹
transform.position += (MoveSpeed + GritySpeed) * Time.fixedDeltaTime;
currentAngle.z = Mathf.Atan((MoveSpeed.y + GritySpeed.y) / MoveSpeed.x) * Mathf.Rad2Deg;
transform.eulerAngles = currentAngle;
}
载抄的 模拟物理抛物线
自己写的基于 Unity物理引擎实现的准确度不高
可以自动追踪目标,准确度不高
代码
简单物理三套
public float speed = 1;
void Start()
{
body = this.GetComponent<Rigidbody>();
body.velocity = this.transform.forward * speed;
print(body.velocity.magnitude);
//body.velocity = new Vector3(50,0,0);
}
炮弹 设置一个初速度即可
控制 控制脚本 炮台 目标 炮弹
Vector3 dir = target.transform.position - rotatepos.transform.position;
Quaternion lookRotation = Quaternion.LookRotation(dir);
Vector3 rotation = Quaternion.Lerp(rotatepos.transform.rotation, lookRotation, 10).eulerAngles;
rotatepos.transform.rotation = Quaternion.Euler(Zvalue, rotation.y, 0f);
time -= Time.deltaTime;
if (time<0)
{
number++;
time = 0.25f;
GameObject pos= Instantiate(game);
pos.transform.position = rotatepos.transform.position;
pos.transform.eulerAngles = rotatepos.transform.eulerAngles;
pos.GetComponent<AAbullet>().speed = number;
print(rotatepos.transform.eulerAngles);
}
}
自己制作的 发现Unity 自带的物理,中 的重力,时间,感觉不太对,
和公式计算的值有一定的相差
Unity 重力自带-20
获取物体速度
这是45度角 产生的值
速度 高度 距离 以大于0高度的为准
2 0.03685282 0.1697056
3 0.09213205 0.3818376
4 0.1719798 0.7353911
5 0.2776855 1.131371
6 0.4085281 1.697056
7 0.5639394 2.276884
8 0.7439191 3.054701
9 0.9491882 3.818377
10 1.180163 4.808325
11 1.435706 5.755846
12 1.715818 6.957926
13 2.020651 8.089303
14 2.345768 9.503513
15 2.707434 11.03087
16 3.087678 12.44508
17 3.492491 14.18456
18 3.923314 15.78263
19 4.379122 17.73423
20 4.859498 19.51614
21 5.364443 21.6799
22 5.894831 23.64563
23 6.450769 26.02154
24 7.031278 28.17111
25 7.636356 30.75917
26 8.266305 33.09257
27 8.922377 35.89273
28 9.60302 38.41003
29 10.30823 41.42231
30 11.03801 44.54773
31 11.79394 47.34792
32 12.57472 50.68546
33 13.38006 53.66936
34 14.20997 57.21901
35 15.06547 60.38698
36 15.94638 64.14875
37 16.85185 67.50036
38 17.7819 71.47422
39 18.73696 75.00998
40 19.718 79.19604
41 20.72361 82.91525
42 21.75378 87.31367
43 22.80852 91.82477
44 23.88958 95.82703
45 24.99532 100.5506
46 26.12562 104.7365
47 27.28049 109.6723
48 28.46111 114.0423
49 29.66697 119.1897
50 30.8974 123.7438
51 32.1524 129.1039
52 33.43258 133.841
53 34.73858 139.4132
54 36.06915 144.3343
55 37.42427 150.1187
56 38.80402 155.2243
57 40.21014 161.2204
58 41.64084 167.3294
59 43.09611 172.7178
60 44.57594 179.0392
61 46.08167 184.6115
62 47.61249 191.1456
63 49.16789 196.9013
64 50.74786 203.6463
65 52.35316 209.5859
66 53.98411 216.5442
67 55.63964 222.6682
68 57.31973 229.8383
69 59.02459 236.1462
70 60.75568 243.5271
71 62.51135 251.0225
72 64.29157 257.6132
73 66.09636 265.3212
74 67.92722 272.0951
75 69.783 280.0138
76 71.66336 286.9715
77 73.5683 295.1042
78 75.49871 302.2462
79 77.45462 310.5894
80 79.43511 317.9164
81 81.44017 326.4704
82 83.47015 333.9812
83 85.5262 342.7489
84 87.60683 350.4416
85 89.71201 359.4234
86 91.84177 366.0821
87 93.99773 370.3405
88 96.1785 373.3521
89 98.38381 377.594
90 100.6137 383.1112
91 102.8692 387.3671
92 105.1501 390.3235
93 107.4556 394.565
94 109.7856 400.136
95 112.1407 404.3951
96 114.5217 407.2928
97 116.9273 411.5365
98 119.3574 417.1637
99 121.8122 421.423
100 124.2933 424.2645
如果 有比较好的 方法或者方式 可以留言谢谢
原文:https://www.cnblogs.com/suiyi78/p/13688668.html