[Scripting] Infinite road : handling rotations correctly
Hi all !
I just started using Unity : it's awesome ! But I'm stuck... here is my problem:
I want to make a car game with an infinite road. I created road segments in 3DS Max. The purpose is to assemble theses segments while the player is driving.
I already have the business logic that "builds" the road while the player car is driving. It works with a little bit of trigger zones, events firing, and a road manager that handles the instantiation of new segments.
My problem is that the instatiation of a new element is not made exactly at the right place.
This is an example of a road segment made with 3DS Max :
It's not explicit on the image, but it is not a straight line : there is a small curve.
So we have a mesh representing our road, and at each extremity there is a cube that is used for 2 things :
detecting when the player enters or leaves a segment to trigger road generation
placing correctly a new segment according to the coordinates of the last one. The idea is to superpose the "start cube" of the new section over the "end cube" of the last section.
So, as i said, i don't succeed in doing the second thing.
He is the script I use :
//Get new road
GameObject currentRoadEndPoint = currentRoad.transform.FindChild ("EndPoint").gameObject;
//Instantiate object at some place, like "I'm doing my best"
int randomOffset = Random.Range (0, availableRoads.Length);
GameObject newRoad = availableRoads [randomOffset];
Vector3 spawnPosition = currentRoadEndPoint.transform.position;
Quaternion spawnRotation = Quaternion.identity;
GameObject createdRoad = Instantiate (newRoad, spawnPosition, spawnRotation) as GameObject;
//Put at right position
GameObject createdRoadStartPoint = createdRoad.transform.FindChild ("StartPoint").gameObject;
createdRoad.transform.rotation = currentRoadEndPoint.transform.rotation * Quaternion.Inverse(createdRoadStartPoint.transform.rotation) * Quaternion.Euler(0, 180, 0);
createdRoad.transform.position = currentRoadEndPoint.transform.position - (createdRoadStartPoint.transform.position - createdRoad.transform.position);
createdRoad.transform.parent = roadsContainer.transform;
roadsQueue.Enqueue (createdRoad);
currentRoad = createdRoad;
The code for rotation is certainly the probleme here. I think I just didn't understand anything about these.
The result is this : http://imgur.com/OmZI0bn
If I display my extremities boxes, I can see that they are not perfectly aligned : http://imgur.com/jK6M6OB
Anyone knows what is happening ?
Your answer
Follow this Question
Related Questions
Rotate player automatically in an infinite runner to follow a winding road C# 1 Answer
Ways to improve endless racing traffic 1 Answer
make a self-driving car, combine Navimesh and waypoint? 1 Answer
How to make rpm increase like this video 0 Answers
Spawning a prefab at a specific point in a procedurally generated map 1 Answer