(スクリプト)
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 isRotatiing = false;
public AudioClip sound;
public GameObject carBody;
public Material[] colors;
private int num = 0;
void Start()
{
line = GetComponen<LineRenderer>();
}
void Update()
{
RaycastHit hit;
Ray ray = new Ray(anchor.position, anchor.forward);
line.SetPosition(0, ray.origin);
if(Physics.Raycast(ray, out hit, maxDistance))
{
line.SetPosition(1, hit.point);
GameObject target = hit.collider.gameObject;
if(OVRInput.GetDown(OVRInput.RawButton.A))
{
if(target.CompareTag("Car"))
{
if(!isRotatiing)
{
AudioSource.PlayClipAtPoint(sound, transform.position);
target.GetComponent<Rotate>().enabled = true;
isRotatiing = true;
}
else
{
AudioSource.PlayClipAtPoint(sound, transform.position);
target.GetComponent<Rotate>().enabled = false;
isRotatiing = 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;
}
}
// ★追加(Boxを持ち上げる)
if(OVRInput.Get(OVRInput.RawButton.RIndexTrigger))
{
if(target.CompareTag("Box"))
{
// 右手(anchor)が「親」 Boxが「子供」
target.transform.parent = anchor;
// BoxのRigidbodyのIs Kinematicを一時的にONにする(ポイント)
target.GetComponent<Rigidbody>().isKinematic = true;
}
}
else
{
// 親子関係の解除
target.transform.parent = null;
// BoxのRigidbodyのIs KinematicをOFFに戻す。
target.GetComponent<Rigidbody>().isKinematic = false;
}
}
else
{
line.SetPosition(1, ray.origin + (ray.direction * maxDistance));
}
}
}