반응형
- 플레이어 코드 작성
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 10f;
public float horizontalBound = 8f;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(horizontalInput * speed, rb.velocity.y);
if (transform.position.x < -horizontalBound)
{
transform.position = new Vector2(-horizontalBound, transform.position.y);
}
if (transform.position.x > horizontalBound)
{
transform.position = new Vector2(horizontalBound, transform.position.y);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Poop"))
{
Debug.Log("Game over!");
Destroy(gameObject);
}
}
}
- 오브젝트 코드 작성
using UnityEngine;
public class PoopController : MonoBehaviour
{
public float speed = 5f;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
rb.velocity = new Vector2(0f, -speed);
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Destroy(gameObject);
}
}
}
- 오브젝트 생성 코드 작성
using UnityEngine;
public class PoopSpawner : MonoBehaviour
{
public GameObject poopPrefab;
public float spawnInterval = 2f;
private float timer = 0f;
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0f)
{
SpawnPoop();
timer = spawnInterval;
}
}
void SpawnPoop()
{
float randomX = Random.Range(-7f, 7f);
Vector2 spawnPosition = new Vector2(randomX, 6f);
Instantiate(poopPrefab, spawnPosition, Quaternion.identity);
}
}
의뢰하기
반응형