找回密码
 注册帐号

扫一扫,访问微社区

外文翻译 如何在Unity中判断位置:简单的小指南

0
回复
1756
查看
[ 复制链接 ]
排名
795
昨日变化

37

主题

820

帖子

3815

积分

Rank: 9Rank: 9Rank: 9

UID
76248
好友
21
蛮牛币
19900
威望
0
注册时间
2015-2-28
在线时间
1146 小时
最后登录
2020-9-7

专栏作家蛮牛译员活力之星七夕浪漫情人

2016-7-8 16:46:17 显示全部楼层 阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册帐号

x
如何在Unity中判断位置:简单的小指南

9761extrapolate-position-in-unity.jpg

Objective
Main objective of this blog post to give you an idea about how to extrapolate position in unity.
目的
本篇博客的主要目的是教会你如何在Unity中判断位置。

Newton's first law of motion:
“An object at rest stays at rest and an object in motion stays in motion with the same speed and in the same direction unless acted upon by an unbalanced force.”
牛顿第一定律:
“静止的物体永远保持静止,运动的物体除非受到不平衡力的作用,会做同一方向的匀速运动。”
Step 1 Newton's First Law
步骤1:牛顿第一定律

Test Newton's first law of motion In Unity:
在Unity中测试牛顿第一运动定律

Take two cubes and name them as CubeUnderUnityPhysics and ExtrapolatedCube.
Add rigid body component to CubeUnderUnityPhysics.
将两块方块命名为“Unity物理系统下的方块”和“推算出的方块”。
为“Unity物理系统下的方块”添加刚体组。

Create an empty game object named PhysicsTest and add script PhysicsTest to it and set both, cube reference and initial velocity of it.
创建一个名为“物理测试”的空游戏对象并添加“物理测试”脚本,并设置它的方块引用和初始速度。

No unbalanced force:
没有不平衡力:

The only condition for this law is no unbalanced force should act upon it. Therefore set gravity and drag it to zero. Gravity can be changed from Edit->Project Settings->Physics.
这条定律的唯一条件是没有不平力的影响。因此设置重力,并将其拖动到零。可以从“编辑”->“项目设置”->“物理”选项更改。

Here, CubeUnderUnityPhysics acts under unity physics engine and position of ExtrapolatedCube is updated by the script and gives its position at any time after the initial velocity is given.
在这里,“Unity物理系统下的方块”被Unity物理引擎影响,而,“推算出的方块”的位置是由给定了初始速度的脚本进行实时更新的。

We know that,
众所周知,

Velocity = distance / time
速度=距离/时间

[C#] 纯文本查看 复制代码
dx=v0x* t
dy=v0y* t
dz=v0z* t


Under this circumstance the position of ExtrapolatedCube is defined by,
在这种情况下,“推算出的方块”的位置取决于,

[C#] 纯文本查看 复制代码
ExtrapolatedCube.position.x = initialVeiocity.x * Time.time;
ExtrapolatedCube.position.y = initialVeiocity.y * Time.time;
ExtrapolatedCube.position.z = initialVeiocity.z* Time.time;


Therefore, CubeUnderUnityPhysics and hence ExtrapolatedCube has constant velocity and no acceleration.
因此,“Unity物理系统下的方块”和“推算出的方块”具有恒定的速度,没有加速度。
Step 2 Apply Gravity
步骤2:接受重力

But what if gravity is applied?
然而,接受重力之后会发生什么?

For constant acceleration motion,
对于加速度恒定的加运动,

[C#] 纯文本查看 复制代码
d= vo t + a t2/2


We know that the gravity of earth is -9.8 m/s2.It means that at every second the velocity is increased by 9.8 in the downward direction.
我们知道,地球的引力是9.8米/ 秒2。这意味着在每一秒的速度是向下的方向增加9.8米/秒。

Gravity is in the downward direction. So, only the y component changes.
重力是竖直向下的,所以,只有Y轴分量产生变化。

[C#] 纯文本查看 复制代码
dx=v0x * t
dy=v0y * t - 9.81 * t2 / 2
dz=v0z * t


For position of ExtrapolatedCube,
“推算出的方块”的位置,

[C#] 纯文本查看 复制代码
ExtrapolatedCube.position.x = initialVeiocity.x * Time.time;
ExtrapolatedCube.position.y = initialVeiocity.y * Time.time +9.81f * Time.time * Time.time / 2 ;
ExtrapolatedCube.position.z = initialVeiocity.z* Time.time;


Note: Make sure that the gravity on Edit->Project Settings->Physics is (0,-9.81,0).
注意:确保”编辑->工程设置->物理“ 的重力值为 (0,-9.81,0)。
Step 3 Change in Gravity
步骤3:改变重力

What if the gravity is other than (0,-9.81, 0)?
如果重力不再是(0,-9.81, 0)将会发生什么?

Suppose we have a game in which the gravity is other than (0,-9.81, 0).
假设我们制作的游戏中重力并非(0,-9.81, 0)。

We need to rewrite the above equation as given below,
我们需要重写如下这些方程式,

[C#] 纯文本查看 复制代码
ExtrapolatedCube.position.x = initialVeiocity.x * Time.time+Physics.gravity.x* Time.time * Time.time / 2;
ExtrapolatedCube.position.y = initialVeiocity.y * Time.time +Physics.gravity.y * Time.time*Time.time / 2;
ExtrapolatedCube.position.z = initialVeiocity.z* Time.time+Physics.gravity.z* Time.time * Time.time / 2;

Step 4 Initial Position is different
步骤4:初始位置不同

What if the initial position of the object is not (0, 0, 0)?
如果初始位置并非(0, 0, 0)将会发生什么?

In this case the equation should be,
在这种情况下,方程式应该是,

[C#] 纯文本查看 复制代码
ExtrapolatedCube.position.x =initialPosition.x+ initialVeiocity.x * Time.time;
ExtrapolatedCube.position.y=initialPosition.y+initialVeiocity.y*Time.time +9.81f * Time.time * Time.time / 2;
ExtrapolatedCube.position.z =initialPosition.z + initialVeiocity.z* Time.time;

Step 5 Extrapolate position after the particular time
步骤5:特定时间后的推断位置

Until now we’ve used time.time as time because we were interested in comparing both the cube positions.
到目前为止我们已经使用Time.time作为时间,因为我们正饶有兴致的比较两个方块的位置。

Now, if we want to know the position of CubeUnderUnityPhysics after any particular time for example after 2 s, then use the following equations:
现在,如果我们想知道“Unity物理系统下的方块”在任何特定的时间的位置,例如在2秒后,然后使用如下的方程:

[C#] 纯文本查看 复制代码
ExtrapolatedCube.position.x = initialPosition.x+ currentVeiocity.x * 2;
ExtrapolatedCube.position.y=initialPosition.y+ currentVeiocity.y*2 + 9.81f * 2 * 2 / 2;
ExtrapolatedCube.position.z = initialPosition.z + currentVeiocity.z* 2;


I hope you find this blog post very helpful while working on Extrapolate Position in Unity. Let me know in comment if you have any questions regarding unity. I will reply you ASAP.
我希望你找到这篇文章很有帮助,在Unity中推算位置。如果你有任何关于Unity的问题,让我在评论中知道。我会尽快回复你。

Got an idea of unity game development what are you still waiting for? Contact us now and see the idea live soon. Our company has been named as one of the best game development company in India.
如果有什么好的Unity游戏想法,不要继续观望!联系我们,立即让我们看到你的想法。我们公司已被评为印度最好的游戏开发公司之一。


免费下载相关源码!

原文作者:Jay Rojivadiya

       原文链接:http://www.theappguruz.com/blog/extrapolate-position-in-unity
扫描下方二维码关注游戏蛮牛官方微信~每日都有精选干货与你分享呦~

本文由蛮牛译馆倾情奉献,翻译:东方喵,如有问题请及时联系,除 合作社区 及 合作媒体 外,禁止转载。



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册帐号

本版积分规则

关注游戏蛮牛公众号送vip