- Home /
How to make looped background?(Character enters left side and appears from right)
Hi! I'm making a 2D that is a copy of the mobile game "Slam Dunk". I'm stuck at one moment. I can't make my background be looped. By "looped" I mean when my ball enters the right side of the screen it appears from the left side like in this video https://www.youtube.com/watch?v=CTDPUMITdf0&t=178s . Can somebody give me an idea or code example or anything please?
Answer by michi_b · Oct 30, 2018 at 06:31 AM
What @Glemau just worte, but I already wrote some code for it, so I'll post it:
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class LoopedRigidbody : MonoBehaviour
{
//world x-coordinate on the left side of the screen where the position should wrap around to and from the other side
[SerializeField]
private float leftWrapAroundBound;
//same as leftBound, just for the right side
[SerializeField]
private float rightWrapAroundBound;
private void FixedUpdate()
{
float loopedAreaWidth = rightWrapAroundBound - leftWrapAroundBound;
Rigidbody2D rigidBody2D = GetComponent<Rigidbody2D>();
//get current position of the rigidbody
Vector2 currentPosition = rigidBody2D.position;
//move rigidbody into screen from the left bound if it is left of the left wrap around bound
while (currentPosition.x < leftWrapAroundBound)
{
//add whole looped areaWidth so it reappears on the right side
currentPosition.x += loopedAreaWidth;
}
//same as I did for the left bound, just mirrored for the right bound
while (currentPosition.x > rightWrapAroundBound)
{
currentPosition.x -= loopedAreaWidth;
}
//write back modified position
rigidBody2D.position = currentPosition;
}
}
I hope this works. Make sure that left bound and right bound are far enough outside of the screen considering your ball size so the teleportation is not obvious. also in your referenced video the ball is never half on the left side, half on the right side of the screen.
Then the solution would be much more complicated. If you have a particle trail like in the referenced movie, you might have to detach it before teleportation and attach a new one afterwards, as particle systems don't like to be teleported in my experience.
I assumed you use Rigidbody2D to make use of Unity's 2D physics, but the example should not be hard to modifiy if that's nto the case.
You probably should also make sure that other colliders (basket) do not poke outside of the screen so the ball never teleports into them. That would probably result in a weird bounce.
Also, that code is written for comprehensibility, not performance :)
Thanks ! This code totally worked for me!(Although i made some adjustments,but not that much). I just set the right and left boundaries as position + the size of the ball, so it looks really natural!
Rather than a while loop you could use modulo for shorter, easier to read code.
currentPosition.x = loopedAreaWidth % currentPosition.x
i think you swapped loopedAreaWidth and currentPosition.x on the right side of your equation? Even if you fix that, that works only if leftWrapAroundBound is 0. And even then, it would not work so easy for negative positions, because somethingNegative % somethingPositive remains negative (for whatever reason). I'm not sure if the modulo way is actually faster at the end, but the main reason i did not use it because this might look more confusing to a beginner:
float positionInLoopedArea = currentPosition.x - leftWrapAroundBound;
positionInLoopedArea = (positionInLoopedArea % loopedAreaWidth + loopedAreaWidth) % loopedAreaWidth;
currentPosition.x = leftWrapAroundBound + positionInLoopedArea;
Answer by Glemau · Oct 30, 2018 at 06:06 AM
You could just set the balls x position to a position just outside the other side of the screen if it leaves the screen. I could give you some code for it in a couple of hours if you want.
Thanks! I got the idea and thanks to @nurPunkt$$anonymous$$ichi i got the code and implemented it into my game!