简体   繁体   中英

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. I know how to move an object from one place to another. 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). 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

Goal 1: Vehicle needs to identify the roads.

Goal 2: Vehicle should take turns automatically depending upon its next set of coordinates.

Goal 3: Vehicle needs to move naturally. 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. You can write your own A* algorithm it's not difficult and you'll learn coding. 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. 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.

You will need to apply car.transform.localPosition(xCorrdinate, yCorrdinate) taking for reference the values you have on the excel sheet. Put that on the update() method, and add a delay you want so the transition from one point to another can be seen.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM