오늘 한 일
Obstacle.cs를 보며 말씀해드리겠습니다. 주석으로 설명을 적어놨습니다.
using System.Collections;
using System.Collections.Generic;
using System.Security;
using UnityEngine;
public class Obstacle : MonoBehaviour
{
public float highPosY = 1f;
public float lowPosY = -1f;
//장애물 사이 구멍크기를 결정합니다.
public float holeSizeMin = 1f;
public float holeSizeMax = 3.5f;
//각각의 위치를 localposition으로 설정을 합니다.
public Transform topObject;
public Transform bottomObject;
GameManager gameManager;
//장애물끼리의 간격
public float widthPadding = 4f;
//GameManager에 싱글톤을 가져옵니다.
private void Start()
{
gameManager = GameManager.Instance;
}
//이전 장애물 위치를 기준으로 랜덤하게 장애물을 생성합니다.
public Vector3 SetRandomPlace(Vector3 lastPosition, int obstacleCount)
{
float holesize = Random.Range(holeSizeMin, holeSizeMax);
float halfHoleSize = holesize / 2;
topObject.localPosition = new Vector3(0, halfHoleSize);
bottomObject.localPosition = new Vector3(0, -halfHoleSize);
Vector3 placePosition = lastPosition + new Vector3(widthPadding, 0);
placePosition.y = Random.Range(lowPosY, halfHoleSize);
//장애물의 위치를 최종적으로 결정하고 반환합니다.
transform.position = placePosition;
return placePosition;
}
//점수기능을 추가합니다.
private void OnTriggerEnter2D(Collider2D collision)
{
Player player = collision.GetComponent<Player>();
if (player != null)
gameManager.AddScore(1);
}
}
다음은 Player.cs입니다.
//update에 죽었을때와 살아있을때 구분해서 출력을 해줍니다.
void Update()
{
if (!isDead)
{
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
isFlap = true; // 살아있을 때 flap
}
if (isDead)
{
deathCooldown -= Time.deltaTime;
if (deathCooldown <= 0)
{
//게임 재시작
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
gameManager.RestartGame();
}
}
}
}
//여기서는 물리이동을 처리해줍니다.
private void FixedUpdate()
{
if (isDead) return;
Vector3 velocity = _rigidbody.velocity;
velocity.x = forwardSpeed;
if (isFlap)
{
velocity.y += flapForce;
isFlap = false;
}
_rigidbody.velocity = velocity;
float angle = Mathf.Clamp((_rigidbody.velocity.y * 10f), -90, 90);
transform.rotation = Quaternion.Euler(0, 0, angle);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (godMode) return;
if (isDead) return;
isDead = true;
deathCooldown = 1f;
animator.SetInteger("isDie", 1);
gameManager.GameOver();
//게임오버 처리
}
}
오늘의 트러블 슈팅
<문제발생>
게임을 만들고 Android로 배포를 하려 했지만 Android NDK not found or invalid. Please, fix it in Edit / Unity -> Preferences -> External Tools 라는 문제가 발생
<문제해결>
tool을 들어가 다 깔려있는지 전부 확인 후 다시 체크박스 해제 후 재시동을 함
<결과>
파일에는 전부 문제가 없는걸 확인했으나 여전히 해결중에 있음.