- Home /
2 sprites. Same Script. Different Velocities.
I have two background sprites. They have a velocity moving down. Both have a single script attached.
Two Issues.
The first sprite moves down faster than the second one coming behind creating unwanted space between them.
The first one when respawns at above the second. It overlaps the second sprite that do issue with my color gardients. After the second respawns before first one. It respawns at correct place having that velocity created space between.
Script attached to both is:
public class bgScroll : MonoBehaviour
{
private float bgLength;
// Start is called before the first frame update
void Start()
{
Vector2 S = gameObject.GetComponent<SpriteRenderer>().sprite.bounds.size;
bgLength = S.y/10.6f;
Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
rigidbody2D.velocity = new Vector2(0, Time.deltaTime*-Global.speedOfScroll);
}
// Update is called once per frame
void Update()
{
if (transform.position.y < -bgLength)
{
Vector2 groundOffset = new Vector2(0, bgLength * 2f);
transform.position = (Vector2)transform.position + groundOffset;
}
}
}
Answer by TechTayyab · Apr 18, 2020 at 08:39 PM
Solved it after 5 hours of troubleshooting. My whole game scale was very small. Like in pixels. So I guess Unity was not able to handle that much small scale. Velocity in float was working with very small negotiable error that was visible enough in my small scale game. Upscaling the game did the job.
Your answer
Follow this Question
Related Questions
Velocity working really weird 2 Answers
physics randomness problem 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers