Never had time to write a blog
Yesterday, my carwaypoints plugin was over.
I didn't go home this year, I was alone for the new year.
It's boring, just take a day off and write a few blogs.
My code may be small, but the idea is important.
Don't want to know the friend don't just copy code
Racing games will inevitably rush out of the runway, into puddles, stuck on obstacles .... and other conditions
So the question is, what about the situation?
Players play well, is it because of encountering these conditions to quit the game re-entry?
Of course it is unrealistic, if my words decisively unload the game
And a man who plays games is a brain remnant.
I don't think you want the player to scold you for being a brain cripple, hahaha.
New skills, get up quickly
The usual way to run a racing game is to reset to the center of the track.
It sounds pretty simple, but it's actually a little complicated.
Let's take off our clothes and go straight to the subject
First, we need waypoint.
I just finished this plugin yesterday.
Carwaypoint Plugin Tutorial as well: http://www.cnblogs.com/shenggege/p/4295616.html
OK, after editing the good road punctuation data, we tidy up the idea of reset function
When the car is reset
We need to get to the nearest sign point from the race.
Then set the front of the car toward the direction of the current nearest road punctuation
Of course, we also have to zero the speed and angular velocity of the car.
To ensure that the car will not automatically forward after reset (you can not return to zero)
The idea is so simple, let's see how the code is implemented
1 ///get the closest path point<summary>2 ///get the closest path point3 /// </summary>4 /// <param name= "DPs" >Path Point Collection</param>5 /// <param name= "Myposition" >Current coordinates</param>6 /// <returns>return a signpost point for the nearest distance</returns>7 PrivateWaypointsmodel Getclosestwp (list<waypointsmodel>All , Vector3 myposition)8 {9Waypointsmodel tMin =NULL;Ten floatMindist = mathf.infinity;//Positive Infinity One A for(inti =0; I < All. Count; i++) - { - floatDist =vector3.distance (All[i]. Position, myposition); the if(Dist <mindist) - { -TMin =All[i]; -Mindist =Dist; + } - } + returntMin; A}
This is a way to get the nearest route punctuation
Calculates the distance of all signpost points from the current race position with a for loop
Take out the minimum distance of the signpost point, this is not difficult to understand, we continue!
1 ///Reset the car<summary>2 ///Reset the car3 /// </summary>4 Private voidRecovercar ()5 {6 //get the nearest signpost point7Waypointsmodel Closestwp =Getclosestwp (Waypointsmodelall, transform.position);8 9 //Place the carTenTransform.position =closestwp.position; One A //front facing -Transform.rotation = quaternion.lookrotation (Closestwp.rotation *vector3.forward); - the //moving speed to zero -Rigidbody.velocity =Vector3.zero; - - //Angular Velocity zeroing +Rigidbody.angularvelocity =Vector3.zero; -}
This is a much simpler approach, isn't it?
After getting to the nearest signpost point, set the position of the car towards the zero parameter.
That's true. Reset, but run the game run a board found there are still some small problems
What's the problem? Your car ran to a sign point before, but after the reset, actually back to the location of the road punctuation
I don't know if I can understand that, if I don't understand, run your game right away, and you'll find out if you try.
So how are we going to solve this problem?
When we reset, the car is not just reset to the center of the track, but also shorten the distance
To solve this problem, we are writing a new method and modifying the code we just wrote, as follows:
1 ///Reset the car<summary>2 ///Reset the car3 /// </summary>4 Private voidRecovercar ()5 {6 //get the nearest signpost point7Waypointsmodel Closestwp =Getclosestwp (Waypointsmodelall, transform.position);8 9 //Next Signpost Point indexTen intNextindex = Closestwp.index +1; One A //Nearest Signpost Point - Vector3 Nearestpoint; - the //The next reset point index is less than the number of signpost points-1 - if(Nextindex < Waypointsmodelall.count-1) - { - //get two road punctuation points from the nearest point of the car +Nearestpoint =Nearestpoint ( - Closestwp.position, + Waypointsmodelall[nextindex]. Position, A transform.position); at } - Else - { - //the position of the last point between the last point and the beginning -Nearestpoint = Waypointsmodelall[waypointsmodelall.count-1]. Position; - } in -Transform.position =Nearestpoint; toTransform.rotation = quaternion.lookrotation (Closestwp.rotation *vector3.forward); +Rigidbody.velocity =Vector3.zero; -Rigidbody.angularvelocity =Vector3.zero; the } * $ ///get two points from the nearest point of racing<summary>Panax Notoginseng ///get two points from the nearest point of racing - /// </summary> the /// <param name= "Linestart" >Nearest Signpost Point</param> + /// <param name= "Lineend" >Next sign Point</param> A /// <param name= "point" >Racing Position</param> the /// <returns></returns> + PrivateVector3 nearestpoint (Vector3 linestart, Vector3 lineend, Vector3 point) - { $ //Line Direction $Vector3 linedirection = vector3.normalize (Lineend-Linestart); - - //nearest Point the floatClosestpoint = Vector3.dot (Point-Linestart), linedirection); - Wuyi //return to nearest point the returnLinestart + (Closestpoint *linedirection); -}
Ideas to see:
OK, save the code now and try to run the game reset
How, is not automatically calculate the nearest point!
The reset point function is so easy to implement
I do not need to calculate the nearest point in the game, just need to get the nearest road punctuation position.
So the specific implementation of the plan according to your needs to do, here I provide only the idea
First publish this article to the home page, every time the first page will be spaced six hours compared to the egg pain
I'm writing two test articles about racing games, and I'm just pasting the address into this article in a moment.
Unity3d racing game-Reverse detection: Waiting to be updated ........ ..........
Unity3d racing game-lap detection and track length calculation: Waiting to be updated ..... ..... .........
This article link: http://www.cnblogs.com/shenggege/p/4295617.html
Unity3d Racing game-Reset point detection