- Home /
How to add player on road spawn script
Hiii. I am a beginner game developer with no coding experience. I am making a endless car game. I want add player by selecting them from menu. If I change car from menu the road spawn script won't work. Here's my script I used for road spawn : using UnityEngine; using System.Collections; using System.Collections.Generic;
public class RoadGenerator : MonoBehaviour {
// road chunk prefab
public GameObject roadChunk;
// distance between edges of the chunk.
public float chunkLength;
// number of chunks to be activated at a time.
public int drawingAmount = 3;
// reference to player object. it is required to manage chunks on the scene.
[SerializeField]private Transform player = null;
// total number of chunks that actually exist in the scene
[SerializeField]private int numberOfChunks = 7;
// list of references to chunks in the scence
private Queue<Transform> chunks;
// reference to chunk that the player is on
private Transform currentChunk;
private int indexOfCurrentChunk;
private int currentChunkPosition = 0;
private void Awake()
{
InitializeChunksList();
}
private void InitializeChunksList()
{
chunks = new Queue<Transform>();
for (int i = 0; i < numberOfChunks; i++)
{
GameObject _chunk = Instantiate<GameObject>(roadChunk);
_chunk.transform.position = NextChunkPosition();
if (i != 0)
_chunk.SetActive(false);
chunks.Enqueue(_chunk.transform);
}
}
private void Start () {
}
private void FixedUpdate () {
if (!player) return;
// determine the chunk that the player is on
currentChunk = GetCurrentChunk();
indexOfCurrentChunk = GetIndexOfCurrentChunk();
// Manage chunks based on current chunk that the player is on
for (int i = indexOfCurrentChunk; i < (indexOfCurrentChunk+drawingAmount); i++)
{
i = Mathf.Clamp(i, 0, chunks.Count-1);
GameObject _chunkGO = (chunks.ToArray()[i]).gameObject;
if (!_chunkGO.activeInHierarchy)
_chunkGO.SetActive(true);
}
if (indexOfCurrentChunk > 0)
{
float _distance = Vector3.Distance(player.position, (chunks.ToArray()[indexOfCurrentChunk - 1]).position);
if(_distance > (chunkLength * .75f))
SweepPreviousChunk();
}
}
private void SweepPreviousChunk()
{
Transform _chunk = chunks.Dequeue();
_chunk.gameObject.SetActive(false);
_chunk.position = NextChunkPosition();
chunks.Enqueue(_chunk);
}
private Vector3 NextChunkPosition()
{
float _position = currentChunkPosition;
currentChunkPosition += (int)chunkLength;
return new Vector3(0, 0, _position);
}
private Transform GetCurrentChunk()
{
Transform current_chunk = null;
foreach (Transform c in chunks)
{
if (Vector3.Distance(player.position, c.position) <= (chunkLength / 2))
{
current_chunk = c;
break;
}
}
return current_chunk;
}
private int GetIndexOfCurrentChunk()
{
int index = -1;
for (int i = 0; i < chunks.Count; i++)
{
if ((chunks.ToArray()[i]).Equals(currentChunk))
{
index = i;
break;
}
}
return index;
}
}
And sorry for my spelling..
Answer by iveL_newO · Apr 29, 2020 at 08:06 AM
It looks like your script is referencing your player's transform. Depending on how you switch cars that might not be updated when you switch.
If you are instantiating a new player object then your script won't be able to find its transform because it will still be looking at the old cars transform. You could fix this by changing your "player" transform if you switch cars.
Your answer
Follow this Question
Related Questions
GUI Follow RaycastHit 2 Answers
How to make a 3d object act like GUI? 1 Answer
Damage script not working? 1 Answer
Football penalty game- Move gameobject in direction of swipe with force 0 Answers
Character won't jump script help. 1 Answer