unity npc길찾기 (야매) + 낮밤 시스템

littleDev·2024년 5월 31일

Unity

목록 보기
3/7
post-thumbnail

MoveAI script

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using Random = UnityEngine.Random;

public class MoveAI : MonoBehaviour
{
    [SerializeField] private GameObject player;
    private Rigidbody2D rigid;
    public int nextMoveX;
    private int nextMoveY;
    private float moveSpeed = 0.4f;
    private Vector2 decisionTime = new Vector2(1, 4);
    private float randomTimeCount = 0f;
    private int currentDir;
    private Transform thisTransform;
    
    private Vector2[] moveDir = new Vector2[]
        { Vector2.up, Vector2.down, Vector2.right, Vector2.left, Vector2.zero, Vector2.zero };
    
    private void Start()
    {
        thisTransform = this.transform;
        randomTimeCount = Random.Range(decisionTime.x, decisionTime.y);
        rigid = GetComponent<Rigidbody2D>();
        Invoke(nameof(Think), 2f);
    }

    private void FixedUpdate()
    {
        // Debug.Log(currentDir);
        thisTransform.position = new Vector2(transform.position.x, transform.position.y) + moveDir[currentDir] * (Time.deltaTime * moveSpeed);
        
        Vector2 DirVec = new Vector2(rigid.position.x + 0.2f, rigid.position.y);
        RaycastHit2D rayHit = Physics2D.Raycast(DirVec, moveDir[currentDir], 1, LayerMask.GetMask("Obstacle"));
        
        Vector2 leftVec = new Vector2(rigid.position.x - 0.2f, rigid.position.y);
        RaycastHit2D rayHitLeft = Physics2D.Raycast(leftVec, Vector2.left, 1, LayerMask.GetMask("Obstacle"));

        Vector2 rightVec = new Vector2(rigid.position.x + 0.2f, rigid.position.y);
        RaycastHit2D rayHitRight = Physics2D.Raycast(rightVec, Vector2.right, 1, LayerMask.GetMask("Obstacle"));
        
        Vector2 upVec = new Vector2(rigid.position.x - 0.2f, rigid.position.y);
        RaycastHit2D rayHitup = Physics2D.Raycast(leftVec, Vector2.up, 1, LayerMask.GetMask("Obstacle"));

        Vector2 downVec = new Vector2(rigid.position.x + 0.2f, rigid.position.y);
        RaycastHit2D rayHitDown = Physics2D.Raycast(rightVec, Vector2.down, 1, LayerMask.GetMask("Obstacle"));

        
        if (rayHit)
        {
            if ((currentDir == 2 || currentDir == 3) && rayHitLeft && rayHitRight)
            {
                currentDir = Random.Range(0, 2); // 0 for up, 1 for down
            }
            if ((currentDir == 0 || currentDir == 1) && rayHitup && rayHitDown)
            {
                currentDir = Random.Range(2, 4); // 0 for up, 1 for down
            }
            if      (currentDir == 0) currentDir = 1;
            else if (currentDir == 1) currentDir = 0;
            else if (currentDir == 2) currentDir = 3;
            else if (currentDir == 3) currentDir = 2;
            Debug.Log("Detected Obstacle");
            CancelInvoke();
            Invoke(nameof(Think), 3f);
        }
    }

    void Think()
    {
        currentDir = Mathf.FloorToInt(Random.Range(0, moveDir.Length));
        Invoke(nameof(Think), Random.Range(4, 8));
    }
}

Day and Night Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.Rendering.Universal;
using UnityEngine.UI;

public class DayAndNight : MonoBehaviour
{
    [SerializeField] private float secondPerRealTimeSecond; // 게임 세계에서의 100초 = 현실 세계의 1초
    [SerializeField] private TextMeshProUGUI tmp;
    private int seconds, minutes, hours, days;
    float gameTime;
    private Light2D _light2D;
    private Color sunrise;
    public Color daytime;
    public Color sunset;
    private bool isDayoff = false;
    
    void Start()
    {
        _light2D = GetComponent<Light2D>();
        sunrise = _light2D.color;
    }

    void Update()
    {
        gameTime += Time.deltaTime* secondPerRealTimeSecond;
        hours = ((int)(gameTime / 3600) + 6) % 24;
        minutes = (int)(gameTime / 60 % 60);
        seconds = (int)(gameTime % 60);
        tmp.text = "day "+days + ", " + hours + ":" + minutes + ":" + seconds;

        if (hours >= 6 && hours <= 11)
        {
            float t = (hours - 6) / 6f + minutes / 360f + seconds / 21600f;
            _light2D.color = Color.Lerp(sunrise, daytime, t);
        }
        else if (hours > 11 && hours <= 17)
        {
            float t = (hours - 12) / 6f + minutes / 360f + seconds / 21600f;
            _light2D.color = Color.Lerp(daytime, sunset, t);
        }
        else if (hours >= 18 && hours < 5)
        {
            _light2D.color = sunset;
        }
        if (hours == 2 && !isDayoff)
        {
            days++;
            gameTime = (6 - 6) * 3600; // 6시로 변경 (gameTime을 6시로 설정)
            isDayoff = true;
        }
        else if (hours != 2)
        {
            isDayoff = false;
        }
    }
}
profile
매일 성장하고싶은 개발자

0개의 댓글