(Quest2)ドアの開閉(アニメーション)

(サンプルコード)

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

public class RayController : MonoBehaviour
{
    public Transform anchor;
    private float maxDistance = 100;
    private LineRenderer line;
    private bool isRotating = false;
    public AudioClip sound;
    public GameObject carBody;
    public Material[] colors;
    private int num = 0;

    // ★追加1(ドアの開閉)
    public Animator carDoorAnim;
    private bool isClose = true;

    void Start()
    {
        line = GetComponent<LineRenderer>();
    }

    void Update()
    {
        RaycastHit hip;

        Ray ray = new Ray(anchor.position, anchor.forward);

        line.SetPosition(0, ray.origin);

        if (Physics.Raycast(ray, out hip, maxDistance))
        {
            line.SetPosition(1, hip.point);

            GameObject target = hip.collider.gameObject;

            if (OVRInput.GetDown(OVRInput.RawButton.A))
            {
                if (target.CompareTag("Car"))
                {
                    if (!isRotating)
                    {
                        AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
                        target.GetComponent<Rotate>().enabled = true;
                        isRotating = true;
                    }
                    else
                    {
                        AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
                        target.GetComponent<Rotate>().enabled = false;
                        isRotating = false;
                    }
                }
            }

            if (OVRInput.GetDown(OVRInput.RawButton.B))
            {
                if (target.CompareTag("CarBody"))
                {
                    AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);

                    Renderer renderer = target.GetComponent<MeshRenderer>();

                    Material[] mats = renderer.materials;

                    num = (num + 1) % colors.Length;

                    mats[2] = colors[num];

                    renderer.materials = mats;
                }
            }

            // ★追加1(ドアの開閉)
            if (OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger))
            {
                if (target.CompareTag("RightDoor") && isClose)
                {
                    carDoorAnim.SetTrigger("DoorOpen");
                    isClose = false;
                }
                else if (target.CompareTag("RightDoor") && !isClose)
                {
                    carDoorAnim.SetTrigger("DoorClose");
                    isClose = true;
                }
            }
        }
        else
        {
            line.SetPosition(1, ray.origin + (ray.direction * maxDistance));
        }
    }
}