- Home /
How to make sure spawned items match scrolling background speed?
Hello,
I'm trying to make a 2D game where the background is scrolling and good and bad things spawn in the front, and those items are suppose to match the scrolling background speed, which is increasing over time.
I want to match everything pixel perfect, and I just can't in no way.
BGScroller code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BGScroller : MonoBehaviour
{
public float tileSizeZ;
public static float scrollSpeed = 1f;
public static float maxScrollSpeed = 15f;
private Vector3 startPosition;
void Start()
{
startPosition = transform.position;
}
void Update()
{
if (scrollSpeed < maxScrollSpeed)
{ scrollSpeed += Time.deltaTime / 10; }
float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ);
transform.position = startPosition + Vector3.left * newPosition;
}
}
Spawned item mover
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour {
private static float startSpeed;
private Rigidbody2D rb;
private void Start()
{
startSpeed = BGScroller.scrollSpeed;
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
rb.velocity = transform.right * -startSpeed *2f;
}
}
I'm very new to game design and programing. the best results I got was if I multiply the spawned item speed by 1.5f, but once the BG reaches like half speed and items don't match the speed. I tried getting the speed from the scroller but that too didn't seem to solve everything, please help.
I forgot to add, my background is a Grid tilemap if that matters.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Gap Between Scrolling Background Objects 0 Answers
Side scroller spawns at different Y 1 Answer
How do I clear my screen after changing resolution? 1 Answer