- Home /
Traffic Cars Spawning Problem
Hello Guys,
I'm having some trouble in instantiating cars in an endless car game like Traffic Racer. I'm trying to spawn my traffic cars at Last Tile of my TilesPool. Cars are spawning fine, but they spawn at same position some times and due to rigidbody they start flying. I want to avoid this . I want the cars to be instantiated at different location on z.
private IEnumerator Start()
{
_player = GameObject.FindGameObjectWithTag(Tags.player);
LastGeneratedCar = _pathGenerator.LastTile;
while (true)
{
GenerateTraffic();
yield return new WaitForSeconds(Random.Range(minValue, maxValue));
}
}
private void GenerateTraffic()
{
if (carRandomValue != _prevCarRandomVal)
{
_prevCarRandomVal = carRandomValue;
var tempCar = Instantiate(DummyCars[carRandomValue],
new Vector3(TrafficRandomPositions[Random.Range(0, TrafficRandomPositions.Length)], // I've four traffic random position assigned from inspector
_pathGenerator.LastTile.transform.position.y,
_pathGenerator.LastTile.transform.position.z + CarInstantiationOffset ), Quaternion.identity) as GameObject;
TrafficIterationCount++;
}
else
{
// Debug.Log("Same Value Generated"); GenerateTraffic(); }
Traffice car that is instantiated is moving along the z axis too. I've used rigidbody.MovePosition in my FixedUpdate of the traffic car.
Please help me out with this. I've been banging my head for some time on this. I know there'd be an easy fix but I'm unable to find that.
Thanks in Advance
Answer by daterre · Jul 27, 2015 at 08:36 AM
You should keep track of _prevTrafficPositionRandomValue, not only _prevCarRandomValue, and make sure you don't generate the same traffic position twice in a row. If they move away faster than the minValue you use in WaitForSeconds, you should be safe.
Thanks for the quick reply. $$anonymous$$y issue is not on the x-axis . $$anonymous$$y issue is on z-axis. TrafficRandomPositions is dealing the x-axis only .
Your answer
Follow this Question
Related Questions
Instantiated projectile always floats up 1 Answer
Use GameObject's global position instead of local position 3 Answers
Rigidbody.velocity works diffrent on cloned object 0 Answers
Instantiated Bullet Force not being applied 0 Answers
Getting instance of an sub object rather than the original's subobject 0 Answers