I am trying to make a 360 Degree Image GoogleCardoard app in unity3d.
I created a Sphere and made 5 material and applied 360 degree image in material. I am swapping material by Raycast and its working but the problem is when crosshair touches the arrow (applied raycast on arrow and crosshair to swap material) the material is changing very fast. i applied a radial progress bar but its not working. What I want is crosshair loading animation everytime when material change
here is my c# code
using UnityEngine;
using System.Collections;
public class next : MonoBehaviour {
public CardboardHead head;
public Material[] myMaterials = new Material[5];
int maxMaterials;
int arrayPos = 0;
public GameObject Sphere;
public int timeToCompleteLoading = 3;
void Start ()
{
head = Camera.main.GetComponent<StereoController> ().Head;
maxMaterials = myMaterials.Length-1;
}
public void ToggleVRMode()
{
Cardboard.SDK.VRModeEnabled = !Cardboard.SDK.VRModeEnabled;
}
IEnumerator RadialProgress(float time)
{
yield return new WaitForSeconds(time);
float rate = 1 / time;
float i = 0;
while (i < 1)
{
i += Time.deltaTime * rate;
gameObject.renderer.material.SetFloat("_Progress", i);
}
}
void Update ()
{
RaycastHit hit;
Collider collider = GetComponent<Collider> ();
if (collider) {
bool isLookAt = collider.Raycast (head.Gaze, out hit, 6.0f);
if (isLookAt == true) {
if (collider.gameObject.tag == "next") {
StartCoroutine (RadialProgress (timeToCompleteLoading));
if (arrayPos == maxMaterials){
arrayPos = 0;
}else{
arrayPos++;
Sphere.renderer.material = myMaterials [arrayPos];
}
}
}
}
}
}
Please Help. Thanks
If you StartCoroutine in Update it will not stop call again StartCoroutine in next frame. You can't delay Update function. But you can use boolean flag to off run part of code:
using UnityEngine;
using System.Collections;
public class next : MonoBehaviour {
public CardboardHead head;
public Material[] myMaterials = new Material[5];
int maxMaterials;
int arrayPos = 0;
public GameObject Sphere;
public int timeToCompleteLoading = 3;
bool wait = false;
void Start ()
{
head = Camera.main.GetComponent<StereoController> ().Head;
maxMaterials = myMaterials.Length-1;
}
public void ToggleVRMode()
{
Cardboard.SDK.VRModeEnabled = !Cardboard.SDK.VRModeEnabled;
}
IEnumerator RadialProgress(float time)
{
wait = true;
yield return new WaitForSeconds(time);
float rate = 1 / time;
float i = 0;
while (i < 1)
{
i += Time.deltaTime * rate;
gameObject.renderer.material.SetFloat("_Progress", i);
}
wait = false;
}
void Update ()
{
RaycastHit hit;
Collider collider = GetComponent<Collider> ();
if (collider) {
bool isLookAt = collider.Raycast (head.Gaze, out hit, 6.0f);
if (isLookAt == true) {
if(wait == false){
if (collider.gameObject.tag == "next") {
StartCoroutine (RadialProgress (timeToCompleteLoading));
if (arrayPos == maxMaterials){
arrayPos = 0;
}else{
arrayPos++;
Sphere.renderer.material = myMaterials [arrayPos];
}
}
}
}
}
}
}