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

public class RecticleMain : MonoBehaviour
{
    public GameObject player;    
    public Transform[] waypoints;
    private int idx;
    public System.Action idxPlus;

    public Car car;
    public UICarMenu uiCarMenu;

    void Start()
    {
        this.idxPlus += () =>
        {
            if (this.idx < 3) this.idx++;
            else this.idx = 0;            
        };

        StartCoroutine(LookAtRoutine());

        this.uiCarMenu.onPointerEnter = (() =>
        {
            //this.car.gameObject.SetActive(true);
            this.uiCarMenu.gameObject.SetActive(true);
            this.player.GetComponent<Player>().movespeed = 0f;
        });

        this.uiCarMenu.onPointerExit = (() =>
        {
            //this.car.gameObject.SetActive(false);
            this.uiCarMenu.gameObject.SetActive(false);
            this.player.GetComponent<Player>().movespeed = 5f;
        });

        this.car.onPointerEnter = (() =>
        {
            //this.car.gameObject.SetActive(true);
            this.uiCarMenu.gameObject.SetActive(true);
            this.player.GetComponent<Player>().movespeed = 0f;
        });

        this.car.onPointerExit = (() =>
        {
            //this.car.gameObject.SetActive(false);
            this.uiCarMenu.gameObject.SetActive(false);
            this.player.GetComponent<Player>().movespeed = 5f;
        });
    }

    IEnumerator LookAtRoutine()
    {     
        //var controller = this.player.GetComponent<CharacterController>();
        //var playerDirection = this.player.GetComponent<Player>();

        while (true)
        {            
            var tpos = new Vector3(this.waypoints[this.idx].position.x, this.player.transform.position.y, this.waypoints[this.idx].position.z);
            //var distance = Vector3.Distance(tpos, this.player.transform.position); 위치로 상태 지정 실패..

            player.transform.LookAt(tpos);

            yield return null;
        }
    }
}

 - 플레이어를 이동시키기 위해 방향백터를 구하는 과정에서 y값을 타겟오브젝트의 좌표로 설정하면 y좌표가 변하면서 일직선으로 움직이지 못한다.

 - 웨이포인트(타겟) 배열의 인덱스를 제어하기 위해 onTriggerEnter 메서드를 사용하였는데 트리거 충돌은 player 오브젝트에서 발생하기 때문에 Action 대리자를 통해 idx 변수 값을 제어하였다.

 

 

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

public class Player : MonoBehaviour
{
    public IEnumerator playerMoveRoutine;
    public float movespeed = 5f;

    void Start()
    {
        this.playerMoveRoutine = PlayerMoveRoutine();
        StartCoroutine(this.playerMoveRoutine);
    }


    public void OnTriggerEnter(Collider other)
    {
        GameObject.Find("RecticleMain").GetComponent<RecticleMain>().idxPlus();
        StopCoroutine(this.playerMoveRoutine);
        StartCoroutine(PlayerStopRoutine());        
    }


    IEnumerator PlayerMoveRoutine()
    {
        var controller = this.GetComponent<CharacterController>();

        while (true)
        {
            controller.Move(controller.transform.forward * this.movespeed * Time.deltaTime);
            yield return null;
        }
    }

    IEnumerator PlayerStopRoutine()
    {
        while (true)
        {            
            yield return new WaitForSeconds(2.0f);
            StartCoroutine(this.playerMoveRoutine);
            break;
        }
    }
}

 

 - 코루틴을 다른 코루틴이나 메서드에서 사용하기 위해 변수에 저장한 후 멤버로 사용

 

 

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


public class Car : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    private EventTrigger evtTrigger;

    public System.Action onPointerEnter;
    public System.Action onPointerExit;
    public System.Action onPointerHover;

    public void Awake()
    {
    }

    public void OnLookAt()
    {
        this.onPointerEnter();
        //Debug.LogFormat("in");
    }

    public void OutLookAt()
    {
        this.onPointerExit();
        //Debug.LogFormat("out");
    }

    public void Looking()
    {
        this.onPointerHover();
    }

    //IPointerEnterHandler 구현
    public void OnPointerEnter(PointerEventData eventData)
    {        
        Debug.LogFormat("OnPointerEnter : {0}", eventData);
    }

    //IPointerExitHandler 구현
    public void OnPointerExit(PointerEventData eventData)
    {
        
        Debug.LogFormat("OnPointerExit : {0}", eventData);
    }

    //IGvrPointerHoverHandler 구현
    public void OnGvrPointerHover(PointerEventData eventData)
    {
        //Debug.LogFormat("OnGvrPointerHover : {0}", eventData);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class UICarMenu : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IGvrPointerHoverHandler
{
    private EventTrigger evtTrigger;

    public System.Action onPointerEnter;
    public System.Action onPointerExit;

    public void Awake()
    {
    }

    public void OnLookAt(bool isLookAt)
    {
        //Debug.LogFormat("lookat : {0}", isLookAt);
        this.onPointerEnter();
    }

    public void OutLookAt(bool isLookAt)
    {
        //Debug.LogFormat("lookat : {0}", isLookAt);
        this.onPointerExit();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {        
        Debug.LogFormat("OnPointerEnter : {0}", eventData);
    }

    //IPointerExitHandler 구현
    public void OnPointerExit(PointerEventData eventData)
    {        
        Debug.LogFormat("OnPointerExit : {0}", eventData);
    }

    //IGvrPointerHoverHandler 구현
    public void OnGvrPointerHover(PointerEventData eventData)
    {
        //Debug.LogFormat("OnGvrPointerHover : {0}", eventData);
    }
}

 - 구글 VR 패키지의 기능

 

 

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

public class EyeCast : MonoBehaviour
{
    private Transform trans;
    private Ray ray;
    private RaycastHit hit;
    public float dist = 10f;

    public Player player;
    public UICarMenu uiCarMenu;

    public GameObject uiGo;
    public Image imgFill;

    void Start()
    {
        this.trans = this.GetComponent<Transform>();


    }

    // Update is called once per frame
    void Update()
    {
        this.ray = new Ray(this.trans.position, this.trans.forward);
        Debug.DrawRay(this.ray.origin, this.ray.direction * this.dist, Color.red);

        var mask = 1 << 8 | 1 << 9;
        if(Physics.Raycast(this.ray, out this.hit, this.dist, mask))
        {
            this.player.movespeed = 0;
            this.uiCarMenu.gameObject.SetActive(true);

            var mask1 = 1 << 9;
            if (Physics.Raycast(this.ray, out this.hit, this.dist, mask1))
            {
                this.uiGo.SetActive(true);

                if (this.imgFill.fillAmount < 1) this.imgFill.fillAmount += 0.001f;
            }
            else
            {
                this.uiGo.SetActive(false);
                this.imgFill.fillAmount = 0f;
            }
        }
        else
        {
            this.player.movespeed = 5;
            this.uiCarMenu.gameObject.SetActive(false);
        }
    }
}

 - 레이캐스트와 오브젝트의 레이어 번호를 비트연산자를 통해서 제어한다.

 

 

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

public class Billboard : MonoBehaviour
{
    private Transform camTrans;
    private Transform trans;

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

    private void LateUpdate()
    {
        this.trans.LookAt(this.camTrans);
    }
}

 - 오브젝트가 항상 타겟 오브젝트를 보도록 함.

+ Recent posts