I’ve been trying to fix an issue I’ve been having with a little endless runner type of game I’m working on for days now.
I decided to go the fixed player and moving terrain route, as I’ve read that letting a transform value get too big can result in loss of precision, but I can’t implement this without having my test player, a cube, “trip” at the seam between two pieces of terrain when they move.
Each piece of terrain is a prefab made like this:
-
Street (contains the Rigidbody, with IsKinematic propety True): this is an empty object containig the:
-
Road (contains MeshRenderer and BoxCollider) - this is where the player “runs”
-
leftSidewalk and rightSidewalk (contains MeshRenderer and BoxCollider)
The terrain spawning script uses the Meshrenderer.bounds.size of the Road object to compute the correct transform to instantiate the following terrain piece. This seems to be correctly working to me because if I test by stopping the terrain and moving the player the “tripping” does not happen.
The issue arises when i actually move all the terrain pieces. In all my attempts I cycle through the array of terrain objects using a foreach statement and apply the same exact movement. I tried applying the movement in different ways:
-
Moving the transform directly:
movement is smooth and tripping does not happen. unfortunately, any obstacle i place on the terrain does not move with it, I think because by moving this way the terrain is not taken into consideration when computing friction. -
Moving the terrain using Rigidbody.MovePosition in Update (using Time.DeltaTime):
this does not cause tripping, but it stutters often especially when i also move the player. -
Moving the terrain using Rigidbody.MovePosition in FixedUpdate:
this causes the player cube to trip (and roll) at each conjunction between two pieces of terrain. visually, I can’t see any gap. I also tried turning IsKinematic to false but in this case the terrain pieces start drifting apart after a few seconds. Also, as i don’t want anything to influence the road, I don’t think this would be good, right?
The only solution I see for now would be to instantiate each terrain object under a parent empty object and move that one, but at that point it would be equivalent to moving the player as far as Transform values go.
I’m at my wits end, and looking for similar projects online I could not find people with the same issue as me.I hope i gave enough info without rabling too much… does anyone have any clue on how to solve this?