- Home /
How to make infinite moving clouds
So I want to have some clouds that slowly move over time and repeat with the camera and I'm have trouble. Should I also have a parallax script on the clouds or not? The cloud script I have right now doesn't move with the camera and I just don't like it too much, any other suggestions on how to do this? This is what I have at the moment.
{
private float speed;
public float minSpeed;
public float maxSpeed;
public float minX;
public float maxX;
private void Start()
{
speed = Random.Range(minSpeed, maxSpeed);
}
private void Update()
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
if (transform.position.x > maxX)
{
Vector2 newPos = new Vector2(minX, transform.position.y);
transform.position = newPos;
}
}
}
Answer by rhapen · Oct 24, 2020 at 09:46 AM
I did repeating terrain script before. If you only change the terrain for clouds it would work just fine i believe. I have 3 terrains with size 500f which always append themselves in front of each other. And If you want the clouds to move with camera/player child the clouds to camera and restrict rotation in editor.
public Transform terrain1;
public Transform terrain2;
public Transform terrain3;
public float speed = 15f;
public float terrainSize = 500f;
public float numberOfTerrains = 3f;
public float origXlocation = -250f;
public float origYlocation = -5f;
float z1;
float z2;
float z3;
Vector3 newposition;
// Update is called once per frame
void Update()
{
newposition = transform.forward * Time.deltaTime * speed;
terrain1.transform.position += newposition;
terrain2.transform.position += newposition;
terrain3.transform.position += newposition;
//Debug.Log(terrain1.transform.position.z);
z1 = terrain1.transform.position.z;
z2 = terrain2.transform.position.z;
z3 = terrain3.transform.position.z;
//go to the end of 1st terain warp back
if (z1 >= terrainSize)
{
z1 = z1 - (numberOfTerrains* terrainSize);
terrain1.transform.position = new Vector3(origXlocation, origYlocation, z1);
}
if (z2 >= terrainSize)
{
z2 = z2 - (numberOfTerrains * terrainSize);
terrain2.transform.position = new Vector3(origXlocation, origYlocation, z2);
}
if (z3 >= terrainSize)
{
z3 = z3 - (numberOfTerrains * terrainSize);
terrain3.transform.position = new Vector3(origXlocation, origYlocation, z3);
}
}
Thank you for responding but this is for a 3d game. $$anonymous$$y game is 2d, its in the tags. But if you have any idea on how to do the clouds in 2d that would help a lot. :)
oops sorry. $$anonymous$$issed that. But either way you can actually use the same idea just change the vector3 to vector2 z is your x and and instead of locking rotation lock the y position
Your answer
Follow this Question
Related Questions
RigidBody2D velocity speed difference between devices 0 Answers
How to add score after destroying object in unity 2D? 1 Answer
Cant access the Image icon from class 1 Answer
why Lagging from tilemap 0 Answers
Code in void not executing 0 Answers