빗물받는 르탄이 강의숙제
>> 맞으면 점수가 깎이는 빨간색 빗방울 만들기
해당 코드는 빗방울 받기 게임을 완성하면서 만든 Rain.cs코드이다.
using UnityEngine;
public class Rain : MonoBehaviour
{
float size = 1.0f;
int score = 1;
SpriteRenderer renderer;
void Start()
{
renderer = GetComponent<SpriteRenderer>();
float x = Random.Range(-2.4f, 2.4f);
float y = Random.Range(3.0f, 5.0f);
transform.position = new Vector3(x, y, 0);
int type = Random.Range(1, 4);
if(type == 1)
{
size = 0.8f;
score = 1;
renderer.color = new Color(100 / 255f, 100 / 255f, 1f, 1f);
}
else if(type == 2)
{
size = 1.0f;
score = 2;
renderer.color = new Color(130 / 255f, 130 / 255f, 1f, 1f);
}
else if(type == 3)
{
size = 1.2f;
score = 3;
renderer.color = new Color(150 / 255f, 150 / 255f, 1f, 1f);
}
transform.localScale = new Vector3(size, size, 0);
}
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Ground"))
{
Destroy(this.gameObject);
}
else if(collision.gameObject.CompareTag("Player"))
{
Destroy(this.gameObject);
GameManager.Instance.AddScore(score);
}
}
}
이제 여기에 빨강 빗방울을 추가해야 하는데, 빗방울의 종류는 type에 의해서 결정되니 type을 하나 늘려서(범위를 4에서 5로 늘려서 4까지 나올 수 있도록 변경) 다른 종류의 빗방울이 나올 수 있도록 한 뒤, if 문에 4의 경우를 추가하여, 빨간 빗방울의 경우를 추가해줍니다.
int type = Random.Range(1, 5);
빨간 빗방울의 경우 크기는 0.8, 점수는 -5점, 색상은 빨간색이므로 이 조건들에 맞춰서 위의 케이스와 같이 코딩해주면 됩니다.
else if(type == 4)
{
size = 0.8f;
score = -5;
renderer.color = new Color(255 / 255f, 100 / 255f, 100 / 255f, 1f);
}
밑은 전체 코드입니다.
using UnityEngine;
public class Rain : MonoBehaviour
{
float size = 1.0f;
int score = 1;
SpriteRenderer renderer;
void Start()
{
renderer = GetComponent<SpriteRenderer>();
float x = Random.Range(-2.4f, 2.4f);
float y = Random.Range(3.0f, 5.0f);
transform.position = new Vector3(x, y, 0);
int type = Random.Range(1, 5);
if(type == 1)
{
size = 0.8f;
score = 1;
renderer.color = new Color(100 / 255f, 100 / 255f, 1f, 1f);
}
else if(type == 2)
{
size = 1.0f;
score = 2;
renderer.color = new Color(130 / 255f, 130 / 255f, 1f, 1f);
}
else if(type == 3)
{
size = 1.2f;
score = 3;
renderer.color = new Color(150 / 255f, 150 / 255f, 1f, 1f);
}
else if(type == 4)
{
size = 0.8f;
score = -5;
renderer.color = new Color(255 / 255f, 100 / 255f, 100 / 255f, 1f);
}
transform.localScale = new Vector3(size, size, 0);
}
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Ground"))
{
Destroy(this.gameObject);
}
else if(collision.gameObject.CompareTag("Player"))
{
Destroy(this.gameObject);
GameManager.Instance.AddScore(score);
}
}
}