728x90
포인터를 따라서 Player가 navi를 이용하여 움직임

car의 collider에 마우스 포인트가 닿으면 커졌다가 떼면 다시 작아짐


Player
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] private Transform[] wayPoints;
[SerializeField] protected float moveSpeed = 1f;
protected Transform nextPoint;
protected System.Action onMoveComplete;
protected Coroutine coroutine;
protected Queue<Transform> queue = new Queue<Transform>();
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()
{
//큐에 넣기 2-3-4-1
for (int i = 1; i < this.wayPoints.Length; i++)
{
this.queue.Enqueue(this.wayPoints[i]);
}
this.queue.Enqueue(this.wayPoints[0]);
}
private void PrintQueue()
{
StringBuilder sb = new StringBuilder();
foreach (var trans in this.queue)
{
sb.AppendFormat("{0} ", trans.name);
}
Debug.Log(sb.ToString());
}
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();
}
}
PlayerAI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerAI : Player
{
[SerializeField] private NavMeshAgent agent;
void Start()
{
Debug.Log("PlayerAI Start()");
//대리자 인스턴스를 생성(메서드를 연결)하고
this.onMoveComplete = () =>
{
Debug.Log("큐를 확인 하고 있으면 큐에서 하나 꺼내서 이동 계속 없으면 이동완료");
this.MoveContinue();
};
this.Init(); //웨이포인트들을 큐에 넣는다
//시작시 큐에서 하나 꺼내고
this.nextPoint = this.queue.Dequeue();
this.Move(); //이동
}
protected override void Move()
{
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;
//Debug.Log(this.agent.remainingDistance);
if (agent.remainingDistance == 0)
{
Debug.Log("이동 완료");
break;
}
}
this.onMoveComplete();
}
}
Car
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
public void OnLookAt(bool isLookAt)
{
//Debug.LogFormat("<color=yellow>isLookAt : {0}</color>", isLookAt);
}
public void OnGvrPointerHover(PointerEventData eventData)
{
Debug.Log("<color=red>hover</color>");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("<color=yellow>enter</color>");
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("<color=green>exit</color>");
}
}
Car에 포인터가 닿으면 멈추게 하고싶다
=> isStopped 사용
true : 멈춤
false : 이동
[SerializeField]
private NavMeshAgent agent;
public void OnLookAt(bool isLookAt)
{
//Debug.LogFormat("<color=yellow>isLookAt : {0}</color>", isLookAt);
}
public void OnGvrPointerHover(PointerEventData eventData)
{
Debug.Log("<color=red>hover</color>");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("<color=yellow>enter</color>");
this.agent.isStopped = true;
}
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("<color=green>exit</color>");
this.agent.isStopped = false;
}

'KDT > VR 콘텐츠 제작' 카테고리의 다른 글
RayExamples Test (0) | 2023.10.27 |
---|---|
Create Ray Interactions (0) | 2023.10.26 |
Oculus 셋팅 (0) | 2023.10.26 |
Create Distance Grab Interactions & Create Ghost Reticles (0) | 2023.10.25 |
Throw an Object (1) | 2023.10.20 |