실행 이미지 1

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Car : MonoBehaviour
{
    public void OnLookAt(bool isLookAt)
    {
        Debug.LogFormat("isLookAt: {0}", isLookAt);
    }
}

car.cs

 

해당 오브젝트를 바라보면 (레이와 콜라이더를 통한 상호작용) isLookAt 로그 상태(true, false) 출력 

 

 

 

+++

 

실행 이미지 2

 

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("isLookAt: {0}", isLookAt);
    }

    public void OnGvrPointerHover(PointerEventData eventData)
    {
        Debug.Log("Hover");
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("enter");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("exit");
    }
}

Car.cs

 

 

Car 오브젝트를 응시하면 Hover, 빠져 나오면 enver이 로그로 출력.

 

처음 응시할 때에는 enter 로그도 출력되는데, Hover가 계속 떠서 이미지엔 보이지 않는다.

 

+++

 

실행 이미지 3

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    private Coroutine coroutine;
    public void OnLookAt(bool isLookAt)
    {
        //Debug.LogFormat("isLookAt: {0}", isLookAt);
    }

    public void OnGvrPointerHover(PointerEventData eventData)
    {
        //Debug.Log("Hover");
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("enter");

       this.coroutine =  StartCoroutine(this.CoClick());
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("exit");

        StopCoroutine(this.coroutine);
    }
    

    IEnumerator CoClick()
    {
        float delta = 0;

        while (true)
        {
            delta += Time.deltaTime;
            Debug.LogFormat("delta = {0}", delta);

            if(delta >3)
            {
                Debug.Log("Clilcked!");
                break;
            }
            yield return null;
        }
    }
}

Car.cs

 

 

Car 오브젝트를 응시하면 코루틴이 시작되고, 

 

3초가 되면 Clicked! 로그를 호출하는 코드 및 실행 이미지

 

+++

 

실행 이미지 4

 

way point의 순서대로 이동하다가 Car 오브젝트를 응시하면 이동을 멈추고, 

 

Car 오브젝트를 응시하지 않으면 다시 이동을 하는 

 

코드 및 실행 이미지

 

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

public class Player : MonoBehaviour
{
   public enum eMoveType
    {
        WayPoint,
        LookAt
    }

    public Transform[] waypoints;
    public eMoveType moveType = eMoveType.WayPoint;
    public float speed = 1.0f;
    public float damping = 3.0f;
    private Transform trans;
    public int nextIdx = 0;

    void Start()
    {
        this.trans = this.transform;
    }

    // Update is called once per frame
    void Update()
    {
        switch (this.moveType)
        {
            case eMoveType.WayPoint:
                this.MoveWayPoint();
                break;
                case eMoveType.LookAt:
                break;
        }
    }

    public void MoveWayPoint()
    {
        Vector3 dir = this.waypoints[nextIdx].position - this.trans.position;
        dir.Normalize();  // 벡터를 정규화합니다.
        Quaternion rot = Quaternion.LookRotation(dir);
        this.trans.rotation = Quaternion.Slerp(this.trans.rotation, rot, Time.deltaTime * damping);

        this.trans.Translate(Vector3.forward * Time.deltaTime * this.speed);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("WayPoint"))
        {
            Debug.Log("Waypoint triggered: " + nextIdx);  // 로그 추가
            this.nextIdx = (++this.nextIdx >= this.waypoints.Length) ? 0 : nextIdx;
        }
    }
}

Player.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    private Player player;

    private void Start()
    {
        player = FindObjectOfType<Player>();
    }
    public void OnLookAt(bool isLookAt)
    {
        //Debug.LogFormat("isLookAt: {0}", isLookAt);
    }

    public void OnGvrPointerHover(PointerEventData eventData)
    {
        //Debug.Log("Hover");
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("enter");
        player.moveType = Player.eMoveType.LookAt;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("exit");

        player.moveType = Player.eMoveType.WayPoint;
    }
    
}

Car.cs

반응형

'산대특 > VR 콘텐츠 기초 및 구현' 카테고리의 다른 글

Locomotion  (0) 2024.04.23
Create a Flat UI  (0) 2024.04.23
Create Ray Interactions  (0) 2024.04.23
Box Grab Surface  (0) 2024.04.19
커스텀 게이지 문제 풀기 수정  (0) 2024.04.16

+ Recent posts