My friend started learning unity and he wanted to make it so every 30 seconds or so , a random background different to the one currently set, would teleport behind the current one, and the current one fades so it looks like it fades to a new background. It would’nt be that hard we thought…
Here is that code…
using UnityEngine;
using UnityEngine.UIElements;
public class Camera_Script : MonoBehaviour
{
public int randomN;
public GameObject firstBackground;
public GameObject seacondBackground;
public GameObject tirstBackground;
public float startTime = 34f;
public float curenttime = 0f;
public int randomNChangeTo;
public Animator PlayGhost1;
public Animator PlayGhost2;
public Animator PlayGhost3;
public Vector3 Offcamera;
public Vector3 Oncamera;
public Vector3 NewBackground;
void Start()
{
Offcamera = new Vector3(50, 0 , 0);
Oncamera = new Vector3(0, 0, 0);
NewBackground = new Vector3(0, 0, 1);
curenttime = startTime;
randomN = Random.Range(1, 4);
print(randomN);
if (randomN == 1)
{
firstBackground.transform.position = Oncamera;
seacondBackground.transform.position = Offcamera;
tirstBackground.transform.position = Offcamera;
}
if (randomN == 2)
{
seacondBackground.transform.position = Oncamera;
firstBackground.transform.position = Offcamera;
tirstBackground.transform.position = Offcamera;
}
if (randomN == 3)
{
tirstBackground.transform.position = Oncamera;
seacondBackground.transform.position = Offcamera;
firstBackground.transform.position = Offcamera;
}
}
void Update()
{
curenttime -= 1 * Time.deltaTime;
if (curenttime <= 1)
{
randomNChangeTo = Random.Range(1, 4);
if (randomNChangeTo != randomN)
{
TransitionBackground();
GhostBeginns();
if (curenttime <= 0)
{
randomNChangeTo = randomN;
Changebackground();
curenttime = startTime;
}
}
}
}
void Changebackground()
{
if (randomN == 1)
{
firstBackground.transform.position = Oncamera;
seacondBackground.transform.position = Offcamera;
tirstBackground.transform.position = Offcamera;
}
if (randomN == 2)
{
seacondBackground.transform.position = Oncamera;
firstBackground.transform.position = Offcamera;
tirstBackground.transform.position = Offcamera;
}
if (randomN == 3)
{
tirstBackground.transform.position = Oncamera;
seacondBackground.transform.position = Offcamera;
firstBackground.transform.position = Offcamera;
}
}
void TransitionBackground ()
{
if (randomNChangeTo == 1)
{
firstBackground.transform.position = NewBackground;
}
if (randomNChangeTo == 2)
{
seacondBackground.transform.position = NewBackground;
}
if (randomNChangeTo == 3)
{
tirstBackground.transform.position = NewBackground;
}
}
void GhostBeginns ()
{
if (randomN == 1)
{
PlayGhost1.Play("PlayFadeOut");
}
if (randomN == 2)
{
PlayGhost2.Play("PlayFadeOut");
}
if (randomN == 3)
{
PlayGhost3.Play("PlayFadeOut");
}
}
}
I just don’t know how to explain the issue…
Also maybe a video could help you visualize the issue.
9264zf
We tried fixing it for 4 hours straight…
our brain dead right now