반응형
- 플레이어 코드 작성
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);
}
}
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# 두번 터치로 카메라 줌인 간단 구현 Camera Zoom in (0) | 2023.04.02 |
---|---|
유니티 C# 화면 캡처하기 간단 구현 Screen Capture (0) | 2023.04.01 |
유니티 C# 일정시간 후 삭제시키기 간단 구현 Destroy (0) | 2023.04.01 |
유니티 C# 지렁이 키우기 게임 만들기 간단 구현 (0) | 2023.04.01 |
유니티 C# 타임라인 Timeline 간단 사용법 (0) | 2023.03.31 |
유니티 C# 물체 회전시키기 Quaternion 종류 및 간단 사용법 (0) | 2023.03.31 |
유니티 C# 지형 자동 생성 Terrain 간단 구현 (0) | 2023.03.31 |
유니티 C# 2d 애니메이션 animation 간단 사용법 (0) | 2023.03.31 |
댓글