简体   繁体   English

如何在 Unity 中自然地将车辆从一个坐标移动到另一个坐标?

[英]How can I move a vehicle in Unity naturally from one coordinate to another?

I am working on a project and I am new to coding.我正在做一个项目,我是编码新手。 The project's goal is to read an excel sheet (contains the front and rear coordinates of the vehicle for the specific time of the simulation) and simulate the resultant animation in Unity.该项目的目标是读取 excel 表(包含模拟特定时间车辆的前后坐标)并在 Unity 中模拟生成的 animation。 I know how to move an object from one place to another.我知道如何将 object 从一个地方移动到另一个地方。 However, I am unable to move the vehicle along the path of the road and reach the final destination.但是,我无法将车辆沿道路移动并到达最终目的地。 Remember that I only have two coordinate positions for the vehicle (one for initial position and the other for the final position).请记住,我只有两个车辆坐标位置(一个用于初始 position,另一个用于最终位置)。 For example, the vehicle needs to take a turn at the intersection automatically if the final destination is set on the adjacent road as can be seen in the image in the below link.例如,如果最终目的地设置在相邻道路上,则车辆需要在交叉路口自动转弯,如下图所示。

https://imgur.com/a/GA86BBn https://imgur.com/a/GA86BBn

Goal 1: Vehicle needs to identify the roads.目标 1:车辆需要识别道路。

Goal 2: Vehicle should take turns automatically depending upon its next set of coordinates.目标 2:车辆应根据其下一组坐标自动轮流。

Goal 3: Vehicle needs to move naturally.目标 3:车辆需要自然移动。 The vehicle should always face the direction in which it moves.车辆应始终面向其移动的方向。 To put it simply, it should move how a vehicle would move in the real world.简而言之,它应该移动车辆在现实世界中的移动方式。

What you're looking for is NavMesh or path finding algorithms.您正在寻找的是NavMesh或路径查找算法。 You can write your own A* algorithm it's not difficult and you'll learn coding.您可以编写自己的 A* 算法,这并不难,您将学习编码。 Then you define places where car can travel or not;然后你定义汽车可以行驶或不能行驶的地方; you'll probably have to do this manually unless you write a script that can extract this information from the map.除非您编写可以从 map 中提取此信息的脚本,否则您可能必须手动执行此操作。 Then you solve using your path finding algorithm and follow the nodes.然后你使用你的寻路算法求解并跟随节点。

There are many explanations online that can help you understand . 网上很多解释可以帮助理解 There are other path finding algorithms as well so check them out.还有其他寻路算法,所以检查一下。

As for facing where it's going, you simple get the direction it's travelling in then rotate its forward vector to match that direction.至于面对它要去的地方,你简单地得到它的行进方向,然后旋转它的前向向量来匹配那个方向。

BTW this isn't your homework solving website.顺便说一句,这不是您的家庭作业解决网站。 You should check out game dev communities if you want basic explanation or search online there is literally hundreds of resources.如果您想要基本解释或在线搜索,您应该查看游戏开发社区,那里有数百种资源。

If you say that you know how to move an object from one place to another, then i don't see the issue.如果您说您知道如何将 object 从一个地方移动到另一个地方,那么我看不到问题所在。

You will need to apply car.transform.localPosition(xCorrdinate, yCorrdinate) taking for reference the values you have on the excel sheet.您需要应用car.transform.localPosition(xCorrdinate, yCorrdinate)以参考 excel 表上的值。 Put that on the update() method, and add a delay you want so the transition from one point to another can be seen.将其放在 update() 方法上,并添加所需的延迟,以便可以看到从一个点到另一个点的转换。

So in the update:所以在更新中:

Update() 
{  
   car.transform.localPosition = Vector2.Lerp(car.transform.localPosition,
      new Vector2(newXCoordinate,newYCoordinate), 
      Time.deltaTime * transitionSpeedTime); 
}

Then add a condition, if you reach to the new destination point, move to the next one.然后添加一个条件,如果到达新的目的地点,则移动到下一个。

Good luck!祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过在运输巨人中设置其路径来将多个车辆从一个站点移动到另一个站点? - How can multiple vehicle be move from one station to another by setting its path as in Transport Giant? 如何通过重复按一个键将 Unity3D 中的 object 从一个地方移动到另一个地方,并按设定的顺序进一步移动? - how can I move an object in Unity3D from one place to another and further in a set order by repeatedly pressing a single key? 在Unity中,如何将值从一个脚本传递到另一个脚本? - In Unity, how can I pass values from one script to another? 如何以编程方式从datagridview中的一个单元移动到另一个? - How can I programmatically move from one cell in a datagridview to another? 如何在 Unity Hololens 中将文件从一个目录移动到另一个目录 - How to move files from one directory to another in Unity Hololens 如何通过 X 坐标在 Unity 中将 GameObjects 居中? - How can I center GameObjects in Unity by X coordinate? 如何在另一个脚本上运行一个方法? [统一 3D] C# - How can I run a method on one script from another? [UNITY 3D] C# 如何在 Unity3d 中将 C# 组件从一个游戏对象传递到另一个游戏对象 - how can I pass a C# component from one gameobject to another in Unity3d Unity:如何将一个 object 放在另一个之上 - Unity: How can I put one object on top of another 从内容页面移至另一个页面时如何清除会话? - how can clear session when i move from a content page to another one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM