first of all this is an endless game. With Raycast, I change direction according to the road in front of me. In other words, if the incoming new tile is to the right, my player also turns to the right. Unfortunately, there is a problem that sometimes when my player touches the new tile, it turns right from the corner, normally it should turn right in the middle of the tile. I will share my codes. public class GroundSpawner : MonoBehaviour {
Vector3 nextSpawnPoint;
public GameObject[] tilePrefabs;
public float TileCount;
public int randomIndex;
public void SpawnTile()
{
Quaternion targetRot = Quaternion.identity;
if(Random.value > 0.5)
{
targetRot = Quaternion.Euler(0, 90, 0);
}
GameObject temp = Instantiate(tilePrefabs[randomIndex], nextSpawnPoint, targetRot);
nextSpawnPoint = temp.transform.GetChild(1).transform.position;
}
void Start()
{
TileCount = tilePrefabs.Length;
SpawnTile();
}
public void ChooseTile()
{
randomIndex = (int)Random.Range(0, TileCount);
print(randomIndex);
}
private void Update()
{
ChooseTile();
}
}
First of all, I have the ground tile script in every plane object.every prefabs has a beginning object , nextspawnpoint object and plane. this planes always being added end to end.
public class GroundTile : MonoBehaviour { GroundSpawner groundSpawner;
void Start()
{
groundSpawner = GameObject.FindObjectOfType<GroundSpawner>();
}
private void OnTriggerEnter(Collider other)
{
groundSpawner.SpawnTile();
Destroy(gameObject, 3f);
}
private void Update()
{
}
}
and now i will share my player controller script. I hope solve this problem , im using this platform first time , to ask my questions. I hope u can help. I analyze my raycast codes , maybe moveVector can be wrong , but i really can not the solution for long time.
private void Update() {
transform.position += moveVector * moveSpeed * Time.deltaTime;
Ray ray = new Ray(transform.position, Vector3.down);
if(Physics.Raycast(ray,out RaycastHit hitInfo,Mathf.Infinity,LayerMask.GetMask("Default")))
{
if (hitInfo.transform.TryGetComponent(out GroundTile tile))
{
// if(turn)
//{
Debug.Log(ray);
moveVector = (tile.transform.position + tile.transform.forward);
// turn = false;
// }
}
}