VioletaBabel

45. 몬스터 공격, 해의 순환 본문

BCA/6. Unity RPG 따라하기
45. 몬스터 공격, 해의 순환
Beabletoet 2018. 6. 20. 14:14

적이 캐릭터와 가까워지면 공격, 멀어지면 다시 패트롤하게 할 것.


===


우선 웨폰 매니저에게 나무를 받아와서 부모를 손으로 두면 나무 막대기를 쥔다.


===


Find를 하고 나면 null 검사를 하는게 좋다. 터지는 것을 방지.

무언가가 죽었다가 다시 살아날 때, active를 껐다 켜도 되지만 destroy하고 다시 instantiate하면 메모리가 달라져서 검사를 하지 않으면 터질 수 있다.


===


NavMeshAgent를 이용해 주인공과 일정 거리 안이면 다가오게 한다.

그리고 공격반경 내에 들면 플레이어의 방향을 보고 공격 모션을 취하도록 한다.


===


[Header("적힐 말")]


을 public 앞줄에 붙이면 묶인다.

그 뒤에 있는 애들이 항목으로 묶인다.



변수값 입력에 슬라이드바도 넣고 싶거든 변수 선언 앞 줄에 

[Range(최소값,최대값)] 을 적으면 된다.

그럼 변수 값의 범위도 제한된다.



[SerializeField]

를 private 변수 앞에 붙이면 인스펙터에는 뜨지만 수정은 불가능.


반대로 [HideInInspector]는 뜨지 않게 해준다. 그리고 이거 변수 하나씩 하기 뭐하면, 같은 타입이면 콤마로 한 번에 만들 시 같이 Hide 된다.



[Space(숫자)] 는 인스펙터에 행간의 여백을 준다. 단, public 변수 앞에 붙여야 한다.



[Tooltip("뜨게 할 말")]은 인스펙터에 설명을 뜨게 한다.



[System.Serializable]

public struct 구조체명

{

변수들

}


을 하면 인스펙터에 클래스로 묶여서 뜬다.


===

해가 뜨고 지는 빛의 변화 표현

Light를 GetComponent하여 color값을 바꿔준다.


아침의 컬러는 95f/255f, 59f/255f, 168f/255f, 1f

낮의 컬러는 0.8f, 0.8f, 0.8f, 1f

저녁의 컬러는 0.75f, 0.45f, 0.55f, 1f

밤의 컬러는 0.35f, 0.35f, 0.35f, 1f


이 코드를 Light에 컴포넌트로 넣으면 됨. 지금은 5초를 기준으로 lightcolors가 한 단계씩 보간을 거치며 바뀜.


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class DayLight : MonoBehaviour
{
 
    private const int maxLightColor = 4;
    private Color[] lightColors = new Color[maxLightColor];
    private float fChangeTime = 0;
    private int nowColor = 0;
    private Color newColor = Color.white;
 
    private Quaternion[] quaternions = new Quaternion[maxLightColor];
    // Use this for initialization
    void Start()
    {
        lightColors[0= new Color(0.372f, 0.231f, 0.658f, 1f); ;
        lightColors[1= new Color(0.8f, 0.8f, 0.8f, 1f);
        lightColors[2= new Color(0.75f, 0.45f, 0.55f, 1f);
        lightColors[3= new Color(0.35f, 0.35f, 0.35f, 1f);
        GetComponent<Light>().color = lightColors[0];
        quaternions[0= new Quaternion();
        quaternions[0].eulerAngles = new Vector3(10600);
        quaternions[1= new Quaternion();
        quaternions[1].eulerAngles = new Vector3(80100);
        quaternions[2= new Quaternion();
        quaternions[2].eulerAngles = new Vector3(60600);
        quaternions[3= new Quaternion();
        quaternions[3].eulerAngles = new Vector3(-90100);
    }
 
    // Update is called once per frame
    void Update()
    {
        fChangeTime += Time.deltaTime * 0.2f;
        if (fChangeTime >= 1.0f)
        {
            fChangeTime = 0;
            nowColor = (nowColor + 1) % 4;
        }
        int nextColor = (nowColor + 1) % 4;
        newColor = Color.Lerp(lightColors[nowColor], lightColors[nextColor], fChangeTime);
        GetComponent<Light>().color = newColor;
        transform.rotation = Quaternion.Lerp(quaternions[nowColor], quaternions[nextColor], fChangeTime);
        RenderSettings.ambientLight = newColor;
    }
}
cs


각도는 임의니까 나중에 알아서 하면 됨.

'BCA > 6. Unity RPG 따라하기' 카테고리의 다른 글

48. NPC와의 대화창 만들기  (0) 2018.06.22
46. 데이터 세이브 로드, HP바, EXP바  (0) 2018.06.21
44. 몹 움직이기  (0) 2018.06.19
43. 몹 패기  (0) 2018.06.18
42. 시야 가리는 벽 반투명  (3) 2018.06.15
Comments