Auto-pathfinding and optimization of key point record (vi) using UNITY3D self-developed racing game example

Source: Internet
Author: User

I. Overview

Racing game of the enemy car auto-pathfinding Generally there are two ways, one is the road point Pathfinding, the other is the use of unity with the component Navmeshagent to find the road, I introduce the latter, the latter in the horizontal plane of the car pathfinding navigation is good, but once there is a Y-direction of climbing, downhill, etc., I've optimized the car because the angle remains level and the tires don't spin.

Two. navmeshagent

This and Navmesh are a pair of game objects used to set up pathfinding. I'm not going to introduce any of the properties, I'll say it from my settings:

1.AgentSize
As in, that cylindrical shape is navmeshagent, wherein the agentsize adjustment to better fit the vehicle on it;

2.Stopping Distance
I set the stopping distance to 0 because I set the seek target to the next road monitoring point each time, if the stopping distance is set to be greater than 0, you will see that the vehicle will slow down acceleration and deceleration acceleration, not coherent;
3.Speed
This I finally found out that its size is m/s, so I set its speed is 47km/h, of course, my game track is more difficult to run, set this value is more appropriate.
4.Auto Traverse Off Mesh Link
This refers to whether the automatic through Offmeshlink, I do not have any special requirements here, do not need some offmeshlink need to set to pass, nature is automatically through the best.
5.Obstacle Avoidance Quality
And then attribute obstacle avoidance quality is the value of the quality of the avoidance of obstacles, for my game, there is only one obstacle, that is, sealed road, such as in the diversion card, this diversion card added components Nav Mesh obstacle, Its role is to use the dynamic as an obstacle, if you hide the component or directly hide the object of the component, the barrier effect is gone, the road in the diagram is again

Three. NavMesh

Navmesh is the navmeshagent on the road to the road, the need to use as the path of the part of the game object set to Navmesh Static, and then Navigation bake bake.
Such as

I'll set all the roads to Navmesh Static, and then bake the blue areas to cover the entire road.
It is important to note that there may be irregularities in the road and that it is necessary to inspect the road itself, and if there are any breaks, the road needs to be paved and re-baked.

Agent information is also required to be used to bake according to the agent, because the difference in agent radius height will result in different baking results.
For example, if you set the agent radius larger, the blue area on the road will be narrower because Navmesh will consider leaving enough distance on both sides of the road.
Then Max Slope's setup takes into account the maximum ramp angle of the vehicle.
The next step is to deal with the area where the fault zone is not connected.

Four. Off Mesh Link

This is a specific connection to the fracture zone, such as

Because the slope is large, unable to penetrate, so use off Mesh Link.
1. First add the component off Mesh Link on the Auto Auto object.
2. Then respectively click on the left side need to connect the plank and the right need to connect the plank, click Navigation, select the Object tab, will appear generate offmeshlinks option to tick, re-bake, such as

3. Drag the left and right plank objects to the start and end properties of the Offmeshlink component of the auto car, so that an arc bridge will appear between the two planks, connecting both ends. This way the road is on, and the vehicle will leap over.

5.navmeshagent.setdestination

It is obviously inappropriate to set up an auto-pathfinding vehicle's pathfinding target as an end point. If you want to find a car, you can follow the established route, you need to constantly update the target point, to a closer position ahead.
I used it when the car hit the first road monitoring point, set the target to the next road monitoring point, so the car will continue to move forward.
Note : I encountered a problem in the development, that is, the car to a point after the stop is not moving. Finally found that the reason is, because the car body navmeshagent, resulting in contact with the next road monitoring point, this time, the road monitoring point has not collided with the car, resulting in the failure to set up the next pathfinding target, the solution is to put the car on the body of the agent set smaller, The car collided with some of the front, so each time the collision was detected first, and has not reached the destination.

6. Fixed the problem that the vehicle is not inclined when the car is downhill

I found that using navmeshagent to navigate the car, the disadvantage is that the car will only turn on the horizontal plane, uphill downhill or horizontal, do not incline along the ramp, I think the reason is because this agent can also be used for the sake of it, because people on the downhill when the body will not be tilted along the ramp.
At first I tried to directly modify the rotation of the car game object and found that the agent has been interfering with it. The solution that comes to mind at the end is:
As the car body/tyre is actually a sub-object of the car object, I can directly modify the angle of the sub-object is good. Will not be interfered with by the agent.
My approach is to set the car's angle, as follows, each time the next pathfinding target of the auto-pathfinding car is setup:

//Set the next pathfinding target point for the enemy auto-pathfinding car    Private voidSetenemycardestionation (int Index)    {//If the current game mode is race mode        if(ConfigurationManager.Instance.CurrentGameModel = = ConfigurationManager.GameModel.RacingModel) {//Get the next road monitoring pointVector3 VEC = Gettheenemyplayernextcollider (Index). Transform.position;//Set the enemy's next pathfinding target _enemyplayermeshagent[index]. Setdestination (VEC);        //Get the current transform of the enemy's bodywork (which is the child of the enemy game object)Transform Currentenemytrans = _enemyplayers[Index].transform;//Calculate the car body facing the next monitoring point, the required number of four Yuanquaternion rotation = quaternion.lookrotation (vec-currentenemytrans.position);//Set the angle so that the vehicle faces the next monitoring point_enemyplayersmanager[Index].        Turncardirection (rotation); }    }
7. Modify the auto-pathfinding car, the wheel does not turn the problem

Since auto-pathfinding is not a physical vehicle, wheelcollider is useless. I see a tutorial on the use of the waypoint method, is to achieve the physical sense of pathfinding.
So I want to add an auto-pathfinding car with a more realistic turning tyre.
That is, as the speed of the car accelerates, the tires turn faster, the speed slows down, and the tires turn slower.
So we need to convert from speed to the current number of degrees per second:
The current car speed is N/s,
Tyre radius is r m
The car speed is N/(2 * R * π)
Car tires per second N/(2*r*π) * 360°
The code is as follows:

    void FixedUpdate()    {        _angularSpeed = (_thisCarNavMesh.velocity.magnitude3600.2f;        0; i < Wheels.Length; i++) {            Wheels[i].Rotate(Vector3.right, _angularSpeed * Time.fixedDeltaTime);        }    }

The parameter 0.2f, is I write according to the effect of the correction parameters, or the angle of the tyre is too large, resulting in poor results.
The above method of my current experimental results are good, but do not know what others think, there must be many other good ways.

Auto-pathfinding and optimization of key point record (vi) using UNITY3D self-developed racing game example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.