- Home /
How do I create a scrolling sky?
I have some clouds that I want to move across the sky. I'm wondering how to write a code that will move them from their starting position and then once they hit the end (essentially because there's only a certain amount of cloud prefabs I have in the scene) will loop continuously and smoothly. Can anyone help?
*In C#
Answer by nug700 · Mar 06, 2017 at 02:24 AM
You spawn the cloud prefab above the map, have a starting point marked, have the cloulds move at a specific rate across the sky. Once They reach beyond a certain point, you set their position to the starting point.
Answer by $$anonymous$$ · Mar 06, 2017 at 02:25 AM
sorry if this code has errors... i havent tested
public Int Multiplyer;
void Update()
{
gameObject.GetComponent<Transform>().Position += Vector3.up * multiplyer
}
change the vector3 to whatever you might need... like Vector3.Down or left or whatever.. Attach this to the cloud
remember to multiply it by Time.deltatime, and you don't need to do gameObject.GetComponent<Transform>().Position
every frame, just do transform.position
, and $$anonymous$$ultiplyer would work better as a float.
Answer by James2Games · Mar 06, 2017 at 05:00 AM
Here is something I wrote a while back that will spawn randomly sized clouds with random speed. Will need a prefab of the cloud and they will spawn around the CloudMaker
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CloudMaker : MonoBehaviour
{
public Cloud cloudTemplate;
public Vector2 spawnHeight = new Vector2(3, 4);
public float SpawnFrequency = 2;
public float SpawnWidth = 200;
private float lastSpawn = 0;
// Update is called once per frame
void FixedUpdate ()
{
if (lastSpawn >= SpawnFrequency)
{
var cloud = Instantiate(cloudTemplate, transform, false);
cloud.Initialize();
var y = Random.Range(spawnHeight.x, spawnHeight.y);
var x = SpawnWidth / 2 * cloud.MoveDirection.x;
var z = SpawnWidth / 2 * cloud.MoveDirection.z;
cloud.transform.position = new Vector3(x, y, cloud.transform.position.z);
lastSpawn = 0;
}
else
{
lastSpawn += Time.fixedDeltaTime;
}
}
}
Attach this to the Cloud Prefab
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Cloud : MonoBehaviour
{
public Vector3 MoveDirection { get { return m_moveDirection; } }
public Vector2 ScaleRange = new Vector2(0.25f, 1);
public Vector2 SpeedRange = new Vector2(0.025f, 0.05f);
public float MaxLife = 3;
private Vector3 m_moveDirection = Vector2.zero;
private float life;
private float moveSpeed;
public void Initialize()
{
// Randomzie speed and scale
m_moveDirection = new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f));
moveSpeed = Random.Range(SpeedRange.x, SpeedRange.y);
float scale = Random.Range(ScaleRange.x, ScaleRange.y);
transform.localScale = new Vector2(scale, scale);
}
IEnumerator Start()
{
yield return new WaitForSeconds(MaxLife);
Destroy(gameObject);
}
void FixedUpdate ()
{
Vector3 move = m_moveDirection * moveSpeed;
transform.position += move;
}
}
Answer by hxg946470289 · Mar 06, 2017 at 10:01 AM
using UnityEngine; using System.Collections;
/// /// 天空盒子的控制 /// public class SkyBoxController : MonoBehaviour { [SerializeField] private Material[] skyBoxMaterials;
/// <summary>
/// 当前天空盒子的索引
/// </summary>
private int skyBoxIndex = 0;
/// <summary>
/// 是否从暗到量
/// </summary>
private bool isToLight = true;
/// <summary>
/// 当前天空盒子的亮度
/// </summary>
private float currentLight = 1;
/// <summary>
/// 天空盒子的最小亮度
/// </summary>
[SerializeField]
private float minLight = 1;
/// <summary>
/// 天空盒子的最大亮度
/// </summary>
[SerializeField]
private float maxLight = 4;
/// <summary>
/// 天空盒子亮度的变化速度
/// </summary>
[SerializeField]
private float changeSpeed = 0.01f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.A))
{
ChangeSkyBox();
}
ChangeSkyBoxArgs();
}
/// <summary>
/// 改变当前天空盒子
/// </summary>
private void ChangeSkyBox()
{
skyBoxIndex++;
if (skyBoxIndex > 3)
skyBoxIndex = 0;
RenderSettings.skybox = skyBoxMaterials[skyBoxIndex];
RenderSettings.skybox.SetFloat("_Rotation", 0);
}
/// <summary>
/// 改变天空盒子的参数
/// </summary>
private void ChangeSkyBoxArgs()
{
if (isToLight)
{
currentLight += changeSpeed;
if (currentLight > maxLight)
isToLight = false;
RenderSettings.skybox.SetFloat("_Exposure", currentLight);
}
if (!isToLight)
{
currentLight -= changeSpeed;
if (currentLight < minLight)
isToLight = true;
RenderSettings.skybox.SetFloat("_Exposure", currentLight);
}
float value = RenderSettings.skybox.GetFloat("_Rotation");
RenderSettings.skybox.SetFloat("_Rotation", value + 0.006f);
}
}