반응형
반응형
코드 작성using UnityEngine;using UnityEngine.UI;public class ScoreController : MonoBehaviour{ public Text scoreText; private int score = 0; void Start() { UpdateScore(); } public void IncreaseScore() //점수 증가 버튼 { score += 1; UpdateScore(); } void UpdateScore() //현재 점수 표시 { scoreText.text = "현재 점수 : " + score; }} 간단한 방치형 클리커 게임 만들기 유니티 C# ..
코드 작성 using UnityEngine; public class DuplicatePrevention : MonoBehaviour { private static bool hasInstance = false; void Awake() { if (hasInstance) { // 이미 인스턴스가 존재하므로 이 인스턴스를 파괴합니다. Destroy(gameObject); } else { // 이 인스턴스가 유일하다는 것을 표시합니다. hasInstance = true; // 다른 씬으로 이동할 때 파괴되지 않도록 설정합니다. DontDestroyOnLoad(gameObject); } } }
유니티 C# 노치 대응하기 SafeArea 에셋 추천 UI 1. 프로젝트 설정먼저 Unity에서 노치 대응을 위한 기본 설정을 합니다.1.1. iOS 설정Project Settings > Player > iOS 탭으로 이동합니다.Resolution and Presentation 섹션에서 Render Outside Safe Area를 체크 해제합니다. 이렇게 하면 iOS의 Safe Area 내부에서만 렌더링이 이루어지게 됩니다.Status Bar 스타일을 원하는 대로 설정할 수 있습니다.1.2. Android 설정Project Settings > Player > Android 탭으로 이동합니다.Resolution and Presentation 섹션에서 Render Outside Safe Area를 체크 해..
코드 작성레이 캐스트를 활용합니다using UnityEngine;public class TouchEvent : MonoBehaviour { void Update() { if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { Debug.Log("터치된 오브젝트: " + hit.transform.name); } ..
코드 작성 using UnityEngine; using UnityEngine.UI; public class Player : MonoBehaviour { public int health = 3; public Image[] healthImages; public void TakeDamage() //공격하기 { health -= 1; healthImages[health].gameObject.SetActive(false); Debug.Log("Player's health: " + health); } } 다른 쪽에서 플레이어 공격할때 Player player = new Player(); player.TakeDamage(); 유니티 기본 UI Slider를 사용하여 구현하기 using UnityEngine; usin..
코드 작성using UnityEngine;public class Example : MonoBehaviour{ // Rigidbody 컴포넌트 Rigidbody rb; // 힘의 크기를 조절하는 변수 public float forceAmount = 10.0f; void Start() { // Rigidbody 컴포넌트 가져오기 rb = GetComponent(); } void Update() { // "스페이스바" 키를 누르면 힘을 적용 if (Input.GetKeyDown(KeyCode.Space)) { // Rigidbody에 지속적인 힘을 가함 rb.A..
유니티 C# 일정거리 범위 안에 목표물 체크 Vector3.Distance코드 작성using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class Example : MonoBehaviour{ public Transform target; float dist; void Update() { dist = Vector3.Distance(target.transform.position, transform.position); if(dist 다른 기능 구현 유니티 C# 플레이어 추적하는 Monster AI 간단 구현코드 작성using Unit..
코드 작성 캐릭터 피격 후 무적 시간 애니메이션으로 사용할 수 있습니다. using System.Collections; using System.Collections.Generic; using UnityEngine; public class Flicker : MonoBehaviour { private SpriteRenderer spriteRenderer; public float delay = 0.5f; //딜레이 public int repeat = 4; //반복 횟수 int value = 0; private void Awake() { spriteRenderer = GetComponent(); } public void Hit() { value = repeat; StartCoroutine(FlickerCorou..
코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowLine : MonoBehaviour { public Transform[] Points; public IEnumerator GetPathEnumerator() { if (Points == null || Points.Length < 1) yield break; var direction = 1; var index = 0; while (true) { yield return Points[index]; if (index = Points.Length - 1) direction = -1; index = index + direction..
코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraController : MonoBehaviour { public float offsetX = 10; public float offsetY = 10; public float offsetZ = 10; void LateUpdate() { transform.position = new Vector3( Mathf.Clamp(Camera.main.transform.position.x, -offsetX, offsetX), Mathf.Clamp(Camera.main.transform.position.y, -offsetY, offsetY)..
코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowCamera : MonoBehaviour { public GameObject target; public float offsetX = 0.0f; public float offsetY = 0.0f; public float offsetZ = 0.0f; Vector3 targetPos; void FixedUpdate() { targetPos = new Vector3(target.transform.position.x + offsetX,target.transform.position.y + offsetY,target.transform..
코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveCamera : MonoBehaviour { public Transform targetPosition; public float smoothTime = 0.3f; private Vector3 velocity = Vector3.zero; public bool isActive = false; private void Start() { isActive = true; } private void Update() { if (isActive) { Camera.main.transform.position = Vector3.SmoothDamp(C..