using UnityEngine;
using System.Collections;
public class SwanTest01 : MonoBehaviour
{
//public GameObject propInstance;
public float speed = -10.0f;
void Start ()
{
// Set the prop's velocity to this speed in the x axis.
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0);
}
}
using UnityEngine;
using System.Collections;
public class SwanTest02 : MonoBehaviour
{
public float speed = -1.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(speed*Time.deltaTime,0,0);
}
}
using UnityEngine;
using System.Collections;
public class BackgroundPropSpawner01 : MonoBehaviour
{
public GameObject propInstance;
public float speed = -10.0f;
void Start ()
{
// Set the prop's velocity to this speed in the x axis.
propInstance.GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0);
}
}
propInstance에 Hierarchy View에 있는 swanSheet를 연결
using UnityEngine;
using System.Collections;
public class BackgroundPropSpawner02 : MonoBehaviour
{
public GameObject backgroundProp;
public GameObject propInstance;
public float speed = -10.0f;
void Start ()
{
propInstance = (GameObject)Instantiate(backgroundProp, transform.position, Quaternion.identity);
// Set the prop's velocity to this speed in the x axis.
propInstance.GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0);
}
void Update ()
{
if(propInstance) {
if(propInstance.transform.position.x < - 20.0f)
// ... destroy the prop.
Destroy(propInstance);
}
}
}
using UnityEngine;
using System.Collections;
public class BackgroundPropSpawner03 : MonoBehaviour
{
public GameObject backgroundProp;
public float leftSpawnPosX; // The x coordinate of position if it's instantiated on the left.
public float rightSpawnPosX; // The x coordinate of position if it's instantiated on the right.
public float minSpawnPosY; // The lowest possible y coordinate of position.
public float maxSpawnPosY; // The highest possible y coordinate of position.public GameObject propInstance;
public float minSpeed; // The lowest possible speed of the prop.
public float maxSpeed; // The highest possible speeed of the prop.
public bool facingLeft;
public GameObject propInstance;
//public float speed = -10.0f;
void Start ()
{
// Set the random seed so it's not the same each game.
Random.seed = System.DateTime.Now.Millisecond;
createSwan();
}
void createSwan() {
// Randomly decide whether the prop should face left or right.
facingLeft = Random.Range(0,2) == 0;
// If the prop is facing left, it should start on the right hand side, otherwise it should start on the left.
float posX = facingLeft ? rightSpawnPosX : leftSpawnPosX;
// Create a random y coordinate for the prop.
float posY = Random.Range(minSpawnPosY, maxSpawnPosY);
// Set the position the prop should spawn at.
Vector3 spawnPos = new Vector3(posX, posY, transform.position.z);
propInstance = (GameObject)Instantiate(backgroundProp, spawnPos, Quaternion.identity);
if(!facingLeft)
{
// ... flip the scale in the x axis.
Vector3 scale = propInstance.transform.localScale;
scale.x *= -1;
propInstance.transform.localScale = scale;
}
// Create a random speed.
float speed = Random.Range(minSpeed, maxSpeed);
// These speeds would naturally move the prop right, so if it's facing left, multiply the speed by -1.
speed *= facingLeft ? -1f : 1f;
// Set the prop's velocity to this speed in the x axis.
propInstance.GetComponent<Rigidbody2D>().velocity = new Vector2(speed, 0);
}
void Update ()
{
if(propInstance) {
if(propInstance.transform.position.x < leftSpawnPosX) {
// ... destroy the prop.
Destroy(propInstance);
createSwan();
}
if(propInstance.transform.position.x > rightSpawnPosX) {
// ... destroy the prop.
Destroy(propInstance);
createSwan();
}
}
}
}
DateTime.Now.Milliseconds : 현재 시간의 1/1000 초 시간대
Random.Range(int a, int b)
a ~ b-1 까지의 랜덤값 반환
Random.Range(float a, float b)
a ~ b 까지의 랜덤 값 반환
Transform.position과 같은 특정 변수는 멤버 변수로 있는 x, y, z 값을 직접 바꿀 수 없고 Vector3 자료형으로 만들어서 position에 바로 대입해야 함