5
\$\begingroup\$

I am creating an endless game, which constantly spawns tiles. The problem is that after a few tiles the player gets stuck on random tile and stops moving. I noticed this problem even when i was designing a level, where when I align two tiles side by side, the player gets stuck on the junction of these two tiles. I am including a video link to my problem. Please help me and please try to be in detail because i very very new to Unity.

Video for the problem im facing. https://photos.app.goo.gl/JrCPWkGclUA2UiMy1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TileManager : MonoBehaviour {

public GameObject[] tiles;
private Transform player;
private float SpawnLocationZ = -30.0f;
private float tileLength = 36;
private float amountOfTilesOnScreen = 5;
private float SafeZone = 45;
private List<GameObject> activeTiles;
private int lastPrefabIndex = 0;
private bool StartCompleted = false;
// Use this for initialization
void Start () {
    activeTiles = new List<GameObject>();
    player = GameObject.FindGameObjectWithTag("Player").transform;
    for(int i =0; i<amountOfTilesOnScreen; i++){
        Debug.Log("Start Method");
        SpawnTile(-1);
    }

}

// Update is called once per frame
void Update () {

    if((player.transform.position.z -SafeZone > (SpawnLocationZ - amountOfTilesOnScreen * tileLength))){
        Debug.Log("started");
        SpawnTile(0);
        DeleteTile();
    }
}

void SpawnTile( int prefabIndex){
    if(prefabIndex == -1){
        GameObject gor;
        gor = Instantiate(tiles[0]) as GameObject;
        gor.transform.SetParent(transform);
        gor.transform.position = Vector3.forward * SpawnLocationZ;
        SpawnLocationZ += tileLength;
        activeTiles.Add(gor);
    }else{
    GameObject go;
    go = Instantiate(tiles[Randome()]) as GameObject;
    go.transform.SetParent(transform);
    go.transform.position = Vector3.forward * SpawnLocationZ;
    SpawnLocationZ += tileLength;
    activeTiles.Add(go);
    }
}

void DeleteTile(){
    Destroy(activeTiles[0]);
    activeTiles.RemoveAt(0);
}

private int Randome(){
    if(tiles.Length <=1){
        return 0;
    }
    int randomIndex = lastPrefabIndex;
    while(randomIndex == lastPrefabIndex){
        randomIndex = Random.Range(0, tiles.Length);
    }
    lastPrefabIndex = randomIndex;
    return randomIndex;
}
 }
\$\endgroup\$
6
  • \$\begingroup\$ It may have something to do with your players collider going so fast it causes some jittery movement which catches on your platforms collider. Maybe try making your player collider a sphere so it is more forgiving when it comes to colliding with your platform. \$\endgroup\$
    – Savlon
    Mar 2, 2018 at 10:46
  • \$\begingroup\$ I agree with Savlon, but have you also checked your tiles z-positions? It might be the case that some objects have a slightly different z-value causing your player object to get stuck on this object. Just try to figure out what the differences are between the two tiles you described in your example, and see what happens when you changes those values arround. \$\endgroup\$
    – sjoerd216
    Mar 2, 2018 at 10:59
  • \$\begingroup\$ I tried them both, since the objects are being spawned from a prefab, im really not sure why there would be any changes in values @sjoerd216 \$\endgroup\$ Mar 2, 2018 at 11:01
  • \$\begingroup\$ @Savlon AWESOME, i dont know if this is the most efficient method but it worked, when i changed the collider, it kept bouncing where it was spposed to stop so i restricted its y position and now its smooth. Thanks a lot \$\endgroup\$ Mar 2, 2018 at 11:05
  • 1
    \$\begingroup\$ Have you read previous questions about getting stuck at the borders of box colliders? ("tile stuck" makes a good search query for turning up a bunch of them) This is a common physics engine quirk you can learn more about in this GDC talk by Bennett Foddy \$\endgroup\$
    – DMGregory
    Mar 2, 2018 at 13:35

1 Answer 1

1
\$\begingroup\$

In many situations, depending on how collision works in your game, you can use the Sphere Colliders or Capsule Colliders instead of the box Colliders. This will let the character slider over Box Colliders without catching.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .