본문 바로가기
VR 콘텐츠 제작

vr mind

by 노재두내 2023. 10. 18.
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UIElements;

public class Player : MonoBehaviour
{
    [SerializeField]
    private Transform[] wayPoints;
    [SerializeField]
    protected float moveSpeed = 1f;
    private int idx = 0;
    protected Transform nextPoint;
    protected System.Action onMoveComplete;
    protected Coroutine coroutine;
    protected Queue<Transform> queue = new Queue<Transform>();
    private Transform firstPoint;
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("player Start");

        this.Init();

        this.onMoveComplete = () =>
        {
            this.MoveContinue();
        };

        this.nextPoint = this.queue.Dequeue();
        this.PrintQueue();
        this.Move();
    }
    protected void MoveContinue()
    {

        if (this.queue.Count > 0)//큐가 비지 않았다면 
        {
            this.nextPoint = this.queue.Dequeue();//꺼내라
            this.PrintQueue();//이름 출력하고
            this.Move();//이동하기
        }
        else
        {
            Debug.Log("모든 이동을 완료했습니다.");//큐가 다 비었다면 더이상 움직이지 않고 출력
        }
    }
    protected void Init()
    {
        //큐에 넣기 1부터 넣어야됨 다음 이동 포인트가 1이니까
        for (int i = 1; i < this.wayPoints.Length; i++)
        {
            this.queue.Enqueue(this.wayPoints[i]);
        }
        this.queue.Enqueue(this.wayPoints[0]);//마지막으로 0 넣기
    }

    private void PrintQueue()
    {
        StringBuilder sb = new StringBuilder();
        foreach (var trans in this.queue)
        {
            sb.AppendFormat("{0}", trans.name);
        }
        Debug.Log(sb.ToString());
    }
    // Update is called once per frame
    //void Update()
    //{

    //    if (Input.GetMouseButtonDown(0))
    //    {
    //        if (idx > 4)
    //        {
    //            idx = 0;
    //            Debug.LogFormat("point{0}로 이동합니다.", this.idx);
    //            StartCoroutine(this.routine);
    //            //Debug.Log("이동을 완료했습니다.");
    //            //StopCoroutine(this.routine);
    //        }
    //        else if(idx<=4)
    //        {
    //            Debug.LogFormat("point{0}로 이동합니다.", this.idx);
    //            this.StartCoroutine(this.routine);
    //            idx++;
    //        }
    //    }
    //}

    protected virtual void Move()
    {
        Debug.LogFormat("{0}으로 이동합니다.", this.nextPoint.name);
        if (this.coroutine != null) StopCoroutine(this.coroutine);
        this.coroutine = this.StartCoroutine(this.CoMove());
    }
    private IEnumerator CoMove()
    {
        while (true)
        {
            this.transform.LookAt(this.nextPoint);
            this.transform.Translate(Vector3.forward * this.moveSpeed * Time.deltaTime);
            var dis = Vector3.Distance(this.nextPoint.position, this.transform.position);
            if (dis <= 0.1f) break;


            yield return null;
        }
        this.onMoveComplete();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerAI : Player
{
    [SerializeField]
    private NavMeshAgent agent;

    private void Start()
    {
        Debug.Log("AiStart");

        //대리자 인스턴스 생성, 메서드를 연결
        this.onMoveComplete = () =>
        {
            this.MoveContinue();
        };
        this.Init();//큐에 웨이포인트들을 넣는다

        //목표위치 설정
        this.nextPoint = this.queue.Dequeue();

        this.Move(); //이동
    }
    protected override void Move()
    {
        //agent.destination = this.nextPoint.position;
        this.agent.SetDestination(this.nextPoint.position);
        if (this.coroutine != null) StopCoroutine(this.coroutine);
        this.coroutine = this.StartCoroutine(this.CoMove());
    }
    private IEnumerator CoMove()
    {
        while (true)
        {
            yield return null;

            if (agent.remainingDistance == 0)
            {
                break;
            }
        }
        this.onMoveComplete();
    }
}

녹화_2023_10_18_11_54_46_129.mp4
6.36MB

'VR 콘텐츠 제작' 카테고리의 다른 글

UseActive State  (0) 2023.10.25
distanceGrab, Ghost Reticle  (0) 2023.10.25
hand grab pose  (0) 2023.10.24
UI 출력  (1) 2023.10.23
오큘러스 개발 문서 링크  (0) 2023.10.14