Home > database >  When my player should go straight in the middle of the tile, sometimes it goes from the corner of th
When my player should go straight in the middle of the tile, sometimes it goes from the corner of th

Time:09-16

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;
           // }
        }
    }

CodePudding user response:

Instead of using Raycast use collision from the tile. when your player collides with the tile, move your player to the center of that tile, and when it reaches the center of the tile turn it as you want. this way turn happens from the center. if you use raycast it will act immediately and turn will happen before you want, or use a delay to call turning

CodePudding user response:

To fix the player turning early, you are instead going to change the move vector only when you are in the middle of the tile, you can add a trigger collider, so when the player reaches it, then they can change move vector:

Like

private void Update()
{
    transform.position  = moveVector * moveSpeed * Time.deltaTime;
}
private void OnTriggerEnter(Collider obj)
{
     if (obj.CompareTag("the tag on the tile))
     {
          moveVector = obj.transform.forward;
     }
}
  1. In your tile prefabs, they need a trigger that should be thin on x and z, but as tall as the player in y. It should be in the center of the tile.

    For example, it would look like a singular minecraft glass pane (the glass pane represents the trigger collider on the tile) on top of a block (the block represents the tile)

  2. You have to have on eof these on each tile prefab, and each tile must have a tag (or layer will work) that is the same for each tile

  • Related