Hey guys , so i have an issue with endless runner , so i have a prefab of map which is instantiated when player touches the trigger.
So the thing is , i don’t know how to make it spawn correctly , i tried taking position of first instantiated prefab and add it’s length, but , map itself moves , so it changes it’s x position every second , also i have “gas pedal” button (when pressed ,map’s speed movement increases), and because of that i have a gap between instantiated maps
Usually gap’s size differ ( because of speed) , here’s my code:
distance = Instantiated.transform.position.x ;
void OnTriggerEnter(Collider other)
{
if (other.tag == “Player”)
Instantiated = Instantiate(swapped, new Vector3(distance + 90, 0, 0), Quaternion.identity) as GameObject;
Just thinking off the top of my head, so its just a thought as im not at pc to have a test.
Try putting your distance inside of the OnTriggerEnter. Might have something to do with the timestep difference between framerate and physics step calls.
It depends where you are finding the distance value and what has passed before using it.
Probably better to put it in that collision call since it will all be done at the same timestep.
Hey, thanks for your reply. I put my distance in OntriggerEnter void , it works, but still there’s a gap , as i said previously because of the gas pedal (speed constantly changes), i was thinking about creating a loop , which is going to subtract prefab’s x position, until there’s no gap, but i don’t know how to put it together and make it work, if you have any suggestions , i would really appreciate.
Thanks for reply again , trigger is a part of prefab!
Here’s my code for map movement :
void Update()
{
speed = speedButt.GetComponent().speed;
transform.Translate(-speed, 0f, 0f);
}
And there’s a button code (gas pedal) :
void addspeed()
{
if (map.GetComponent().dontGo != 0)
{
if (ispressed)
{
if (speed <= 1.0f)
speed += 0.01f;
}
else if (!ispressed)
{
if(speed>=0.5f)
speed -= 0.01f;
}
}
}
I did a similar thing in 2d yesterday and also got gaps. I “solved” it by introducing a 10% overlap (spawning slightly early) which seemed to be enough to compensate for the variance.
Hey @phrobion , thanks for reply . It’s not really one of the courses , just my little project. if i understood you correctly , i didn’t really use collision , i used OntriggerEnter , when my “player” touches the trigger , it’s instantiates a prefab.
Sure, keep in mind I hacked this before bed last night and I haven’t tested it over a long run to see if problems creep in, but it seems to work ok in the editor.
using UnityEngine;
namespace NinjaChimpStudios.FreezoRun.Scripts.Game {
public class TiledSpriteSpawner : MonoBehaviour {
public TiledSprite TheTiledSprite;
public float Overlap = 1.0f;
private float _spriteDeltaTime;
private float _lastSpawn;
void Start() {
_spriteDeltaTime = TheTiledSprite.GetComponent<SpriteRenderer>().bounds.size.x / TheTiledSprite.Speed * Overlap;
}
void Update() {
if ((Time.time - _lastSpawn) > _spriteDeltaTime) {
Instantiate(TheTiledSprite, transform.position, Quaternion.identity);
_lastSpawn = Time.time;
}
}
}
}
So this is my generic tiled sprite spawner, it basically instantiates sprites at an appropriate interval so they are tiled as they move (for seamelss moving parallax backgrounds). The spawn rate depends on the speed (configured in the sprite prefab) but as I was getting occasional gaps of a pixel or two I introduced the configurable overlap. I set it to 0.9 in the editor, which makes the new sprite spawn 10% earler. Hope it helps.
You could create an empty gameObject at the beginning of the track GameObject Prefab and another one at the end of it and use it as reference points when instantiating
Today I learned sth in here about unity.
Confused me, but seems legit.
(Your question made me to quest the following post)
If you got a trigger, you got a collider.
In your case one collider is that “wall”.
I assume that rigidbody is the car (player) .
So: Does your player use “discrete” or “continous” collosion-detecting?
I hope for a win-win-situation.
Your problem solved. My question answered.
Hey , yes you’re right , my player have rigidbody with discrete collision detecting , unfortunately changing it to continuous doesn’t fix the problem, although the gap decreases it’s size, but i’ll try something else , thanks anyway!
Hey, thanks for reply . I changed my collision detecting to continuous , the gap decreases it’s size, but as i said it’s hard to check , because the size of the gap is always different , as speed constantly changes
I tried to avoid, in my code, any use of specific offsets and relied on the fact that the collider that triggers the spawning of the new sprite is always in a specific known position, and used it to calculate the spawning point of the new sprite.
Infos about the variables used:
PlayerTransform is the player transform used to check if the collider is triggered
SpriteToInstantiate, self explanatory
ParentTransform is the Transform of the Map container game object (pic below)
It works without problems even with the collision detection set to Discrete and a 0.02 Fixed Update frequency, up until ungodly movement speeds ( 100 * Time.deltaTime works, at 200* I need to increase the Fixed Update frequency).