반응형
코드 작성
PlayerController
using UnityEngine;
// 플레이어 컨트롤러 클래스
public class PlayerController : MonoBehaviour
{
// 이동 속도
public float speed = 5.0f;
// 게임 오브젝트 업데이트 메서드
void Update()
{
// 수평 및 수직 입력 받아오기
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// 이동 벡터 계산
Vector3 movement = new Vector3(horizontal, 0, vertical);
// 플레이어 이동
transform.Translate(movement * speed * Time.deltaTime);
}
// 적과 충돌 감지 메서드
void OnCollisionEnter(Collision collision)
{
// 적과 충돌 시
if (collision.gameObject.CompareTag("Enemy"))
{
// 적 제거
Destroy(collision.gameObject);
}
}
}
적 생성
using UnityEngine;
// 적 생성기 클래스
public class EnemySpawner : MonoBehaviour
{
// 적 프리팹
public GameObject enemyPrefab;
// 생성 주기
public float spawnInterval = 2.0f;
// 게임 오브젝트 시작 시 호출되는 메서드
void Start()
{
// 적 생성 반복 실행
InvokeRepeating("SpawnEnemy", 0, spawnInterval);
}
// 적 생성 메서드
void SpawnEnemy()
{
// 무작위 위치 지정
Vector3 spawnPosition = new Vector3(Random.Range(-10, 10), 0, Random.Range(-10, 10));
// 적 생성
Instantiate(enemyPrefab, spawnPosition, Quaternion.identity);
}
}
적 인공지능 Ai
using UnityEngine;
// 적 AI 클래스
public class EnemyAI : MonoBehaviour
{
// 플레이어 타겟
private GameObject player;
// 이동 속도
public float speed = 3.0f;
// 게임 오브젝트 시작 시 호출되는 메서드
void Start()
{
// 플레이어 오브젝트 찾기
player = GameObject.FindGameObjectWithTag("Player");
}
// 게임 오브젝트 업데이트 메서드
void Update()
{
// 플레이어 방향 벡터 계산
Vector3 direction = (player.transform.position - transform.position).normalized;
// 적 이동
transform.Translate(direction * speed * Time.deltaTime);
}
}
다른 간단한 게임 만들기
유니티 C# 간단한 타워 디펜스 게임 만들기 Tower Defense
적 코드using UnityEngine;public class Enemy : MonoBehaviour{ public float speed = 5f; // 적의 이동 속도 void Update() { Move(); // 이동 함수 호출 } void Move() { // 적이 현재 위치에서 목표 지점으로 이동하는 방향을 계산
parksh3641.tistory.com
유니티 C# 간단한 방치형 클리커 게임 만들기 예시 구현
코드 작성using System.Collections;using UnityEngine;using UnityEngine.UI;public class ClickerGame : MonoBehaviour{ // UI 요소들을 연결할 변수들 public Text scoreText; // 점수를 표시하는 텍스트 public Text perSecondText; // 초당
parksh3641.tistory.com
의뢰하기
유니티로 제작된 게임을 업그레이드 해드립니다. - 크몽
DevPark 전문가의 IT·프로그래밍 서비스를 만나보세요. <p><strong style="font-size: 24px;&q...
kmong.com
반응형