전체 글 (223) 썸네일형 리스트형 [Unity] 플레이어를 이용한 선택적 무한 배경(1) [Unity] 플레이어가 바라보는 방향으로 Ray를 만들어보자의 연장이다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 using System.Collections; using System.Collections.Generic; using UnityEngine; public class BackgroundScrolling : MonoBehaviour { public float speed; public Transform[] backgrounds; float leftPosX = 0f; float rightPosX = 0f; float xScreenHalfSize; float yScreenHalfSize; speed 배경들이 움직일 속도를 정의한 변수이다. backgrounds 배경들을 담아줄 변수.. [Unity] 플레이어가 바라보는 방향으로 Ray를 만들어보자 npc와 대화하는 것을 만들기 위해서 플레이어가 바라보는 방향으로 RayCast를 만들어보겠다. 수평, 수직 방향 이동과 동일한 프로젝트에서 진행되기 때문에 코드를 이어가겠다. 1 2 3 4 5 6 7 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { Vector3 playerDir; 수평, 수직 방향 이동 플레이어 스크립트에서 추가되는 것은 playerDir이다. playerDir - 플레이어가 바라보는 방향에 대한 정보를 저장할 변수이다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 vo.. [Unity] 수평, 수직이동을 시켜보자 플레이어의 Horizontal과 Vertical 입력을 받아서 대각선으로 이동하지 않고, 수평으로만 이동하게 해 보겠다. 1 2 3 4 5 6 7 8 9 10 11 12 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public float speed; Rigidbody2D rigid; bool isHorizontalMove; float hInput; float vInput; speed - 플레이어의 속도를 저장할 변수이다. rigid - 플레이어의 Rigidbody2D를 저장할 변수이다. isHorizontalMove - 수평이동을 하고 있는지.. [도둑과 경호원] Unity: event 적용시키기 System.Action으로 만들었던 이벤트 UI를 Player와 Guard 스크립트에 적용시켜보겠다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class Player : MonoBehaviour { void Start() { Guard.OnGuardSpottedPlayer += Disable; } void OnTriggerEnter(Collider other) { if(other.tag == "Finish") { Disable(); if(OnReachEndOfLevel != null) { OnReachEndOfLevel(); } } } void Disable() { disabled = true.. [도둑과 경호원] Unity: Sysyem.Action을 이용한 이벤트 System.Acition(delegate)을 이용하여 이벤트를 발생시키는 방법에 대해서 알아볼 것이다. 이 방법은 이번에 공부하면서 알게 된 방법이다. 이 방법의 좋은 점은 단지 System.Action에 저장되어있는 함수를 실행하는 것을 목적으로 하기 때문에 Guard나 Player 스크립트가 사라져도 오류를 반환하지 않는다는 점이다. 1 2 3 4 5 6 7 8 9 public class Guard : MonoBehaviour { public static event System.Action OnGuardSpottedPlayer; } public class Player : MonoBehaviour { public static event System.Action OnReachEndOfLevel; } 먼.. [도둑과 경호원] Unity: Guard 플레이어 검거하기 Guard에게 플레이어가 일정 범위 안에 들어갔을 때 플레이어를 검거할 수 있는 로직을 구현해보겠습니다. 1 2 3 4 5 6 7 8 9 10 public Light spotLight; public float viewDistance = 5; public LayerMask viewMask; public float timeToSpotPlayer = 0.5f; float playerVisibleTimer; float viewAngle; Transform player; Color originalSpotlightColor; spotLight - Guard가 가지고 있는 spotLight를 저장할 변수이다. viewDistance - Guard가 플레이어를 검거할 수 있는 직선거리이다. viewMask - Laye.. [도둑과 경호원] Unity: Player 회전과 이동 이번에는 Player가 움직이는 키에 맞게 회전하여 움직이는 방법에 대해 알아보겠다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { public float moveSpeed = 7; public float smoothMoveTime = 0.1f; public float turnSpeed = 8; float angle; float smoothInputMagnitude; float smoothMoveVelocity; bool disabled; Vector3 velocity; Ri.. [도둑과 경호원] Unity: Guard 기즈모 OnDrawGizmos는 개발자가 진행 상황들을 편하게 보기 위해서 Game뷰에 기즈모들이 보이게 해주는 것이다. 이 함수를 사용하여 개발자는 게임뷰를 보면서도 씬뷰를 보는 것과 같이 볼 수 있고 오류는 없는지 보다 정확한 판단을 하게 해줄 수도 있다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void OnDrawGizmos() { Vector3 startPosition = pathHolder.GetChild(0).position; Vector3 previousPosition = startPosition; foreach(Transform waypoint in pathHolder) { Gizmos.DrawSphere(waypoint.position, 0.3f); Gizmos.D.. 이전 1 ··· 20 21 22 23 24 25 26 ··· 28 다음