VR空間でボディのカラーを変更する。車種を変更する。
Oculus Go の入力+Raycast
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayController : MonoBehaviour
{
public Transform anchor;
public float maxDistance = 100;
private LineRenderer lineRenderer;
public AudioClip sound;
private bool isRotating;
public GameObject carBody;
public Material[] bodyColor;
public AudioClip sound2;
private int currentNum = 0;
public GameObject[] cars;
void Start()
{
lineRenderer = GetComponent<LineRenderer>();
}
void Update()
{
if (OVRInput.GetDown(OVRInput.Button.Back))
{
if (lineRenderer.enabled)
{
lineRenderer.enabled = false;
}
else
{
lineRenderer.enabled = true;
}
}
RaycastHit hit;
Ray ray = new Ray(anchor.position, anchor.forward);
lineRenderer.SetPosition(0, ray.origin);
if (Physics.Raycast(ray, out hit, maxDistance))
{
lineRenderer.SetPosition(1, hit.point);
GameObject target = hit.collider.gameObject;
if (OVRInput.GetDown(OVRInput.Button.Back))
{
if (target.tag == "Block")
{
target.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
}
if (target.tag == "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.Button.PrimaryIndexTrigger))
{
if (target.tag == "Car")
{
AudioSource.PlayClipAtPoint(sound2, Camera.main.transform.position);
// ①色を順繰りに変更する。
// 青 赤 黄 黒・・・>以下繰り返し
var render = carBody.GetComponent<MeshRenderer>();
Material[] mats = render.materials;
currentNum = (currentNum + 1) % bodyColor.Length;
mats[2] = bodyColor[currentNum];
render.materials = mats;
// ②車種を順繰りに変更する。
currentNum = (currentNum + 1) % cars.Length;
for (int i = 0; i < cars.Length; i++)
{
if (i == currentNum)
{
cars[i].SetActive(true);
}
else
{
cars[i].SetActive(false);
}
}
}
}
}
else
{
lineRenderer.SetPosition(1, ray.origin + ray.direction * maxDistance);
}
}
}
Unity Code Memo
他のコースを見るOculus Go の入力+Raycast
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayController : MonoBehaviour
{
public Transform anchor;
public float maxDistance = 100;
private LineRenderer lineRenderer;
public AudioClip sound;
private bool isRotating;
public GameObject carBody;
public Material[] bodyColor;
public AudioClip sound2;
private int currentNum = 0;
public GameObject[] cars;
void Start()
{
lineRenderer = GetComponent<LineRenderer>();
}
void Update()
{
if (OVRInput.GetDown(OVRInput.Button.Back))
{
if (lineRenderer.enabled)
{
lineRenderer.enabled = false;
}
else
{
lineRenderer.enabled = true;
}
}
RaycastHit hit;
Ray ray = new Ray(anchor.position, anchor.forward);
lineRenderer.SetPosition(0, ray.origin);
if (Physics.Raycast(ray, out hit, maxDistance))
{
lineRenderer.SetPosition(1, hit.point);
GameObject target = hit.collider.gameObject;
if (OVRInput.GetDown(OVRInput.Button.Back))
{
if (target.tag == "Block")
{
target.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
}
if (target.tag == "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.Button.PrimaryIndexTrigger))
{
if (target.tag == "Car")
{
AudioSource.PlayClipAtPoint(sound2, Camera.main.transform.position);
// ①色を順繰りに変更する。
// 青 赤 黄 黒・・・>以下繰り返し
var render = carBody.GetComponent<MeshRenderer>();
Material[] mats = render.materials;
currentNum = (currentNum + 1) % bodyColor.Length;
mats[2] = bodyColor[currentNum];
render.materials = mats;
// ②車種を順繰りに変更する。
currentNum = (currentNum + 1) % cars.Length;
for (int i = 0; i < cars.Length; i++)
{
if (i == currentNum)
{
cars[i].SetActive(true);
}
else
{
cars[i].SetActive(false);
}
}
}
}
}
else
{
lineRenderer.SetPosition(1, ray.origin + ray.direction * maxDistance);
}
}
}
VR空間でボディのカラーを変更する。車種を変更する。