반응형
코드 작성
PlayerController.cs
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// 이동 속도
public float moveSpeed = 5f;
// 점프 힘
public float jumpForce = 10f;
// 땅 체크
private bool isGrounded;
// 리지드바디 컴포넌트
private Rigidbody2D rb;
// 땅 체크 위치
public Transform groundCheck;
// 땅 체크 반경
public float groundCheckRadius;
// 땅 레이어
public LayerMask groundLayer;
void Start()
{
// 리지드바디 컴포넌트 가져오기
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// 수평 입력 받기
float moveInput = Input.GetAxis("Horizontal");
// 플레이어 이동
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
// 땅 체크
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
// 점프 입력 받기
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
// 점프
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
// 땅 체크 위치 시각화 (디버그용)
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
}
}
GameManager.cs
using UnityEngine;
public class GameManager : MonoBehaviour
{
// 게임 오버 상태
private bool isGameOver = false;
// 플레이어 게임 오브젝트
public GameObject player;
void Update()
{
// 게임 오버 상태 체크
if (isGameOver)
{
// 재시작 입력 받기
if (Input.GetKeyDown(KeyCode.R))
{
// 씬 재시작
UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
}
}
}
public void GameOver()
{
// 게임 오버 설정
isGameOver = true;
// 플레이어 비활성화
player.SetActive(false);
}
}
PlatformController.cs
using UnityEngine;
public class PlatformController : MonoBehaviour
{
// 이동 속도
public float moveSpeed = 2f;
// 이동 방향
private bool movingRight = true;
// 왼쪽/오른쪽 경계
public Transform leftEdge;
public Transform rightEdge;
void Update()
{
// 플랫폼 이동
if (movingRight)
{
transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
// 오른쪽 경계 도달 시
if (transform.position.x >= rightEdge.position.x)
{
movingRight = false;
}
}
else
{
transform.Translate(Vector2.left * moveSpeed * Time.deltaTime);
// 왼쪽 경계 도달 시
if (transform.position.x <= leftEdge.position.x)
{
movingRight = true;
}
}
}
}
다른 간단한 게임 만들기
유니티 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
반응형