(サンプルコード)
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;
// ★追加2(ハンドルのデザインの切り替え)
public GameObject steering;
public Material[] steeringMats;
private int steeringNum = 0;
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;
}
// ★追加2(ハンドルのデザインの切り替え)
if (target.CompareTag("Steering"))
{
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
steeringNum = (steeringNum + 1) % steeringMats.Length;
steering.GetComponent<MeshRenderer>().material = steeringMats[steeringNum];
}
}
}
else
{
line.SetPosition(1, ray.origin + (ray.direction * maxDistance));
}
}
}