Search Unity

Problem With Changing From One Sphere To Another

Discussion in 'AR/VR (XR) Discussion' started by StefanK_93, Apr 11, 2019.

  1. StefanK_93

    StefanK_93

    Joined:
    Apr 10, 2019
    Posts:
    6
    I am currently trying to create an environment where I have two sphere objects where there is a 360 panorama picture in each of them. I created an object in the first sphere which I want to click and then end up in the second sphere. I tried to do this according to a tutorial I found on youtube. But apparently when I click the button nothing happens. Does anybody know what could be the problem?
     
  2. Untoldecay

    Untoldecay

    Joined:
    Apr 9, 2016
    Posts:
    24
    Hey,
    could you provide the link of the tutorial and some parts of your code?

    Cheers
     
  3. StefanK_93

    StefanK_93

    Joined:
    Apr 10, 2019
    Posts:
    6
    Hi,

    sure, that´s the link for the tutorial:



    And that´s the code for the script "Change.Sphere":

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

    public class SphereChanger : MonoBehaviour {



    //This object should be called 'Fader' and placed over the camera
    GameObject m_Fader;

    //This ensures that we don't mash to change spheres
    bool changing = false;


    void Awake()
    {

    //Find the fader object
    m_Fader = GameObject.Find("Fader");

    //Check if we found something
    if (m_Fader == null)
    Debug.LogWarning("No Fader object found on camera.");

    }


    public void ChangeSphere(Transform nextSphere)
    {

    //Start the fading process
    StartCoroutine(FadeCamera(nextSphere));

    }


    IEnumerator FadeCamera(Transform nextSphere)
    {

    //Ensure we have a fader object
    if (m_Fader != null)
    {
    //Fade the Quad object in and wait 0.75 seconds
    StartCoroutine(FadeIn(0.75f, m_Fader.GetComponent<Renderer>().material));
    yield return new WaitForSeconds(0.75f);

    //Change the camera position
    Camera.main.transform.parent.position = nextSphere.position;

    //Fade the Quad object out
    StartCoroutine(FadeOut(0.75f, m_Fader.GetComponent<Renderer>().material));
    yield return new WaitForSeconds(0.75f);
    }
    else
    {
    //No fader, so just swap the camera position
    Camera.main.transform.parent.position = nextSphere.position;
    }


    }


    IEnumerator FadeOut(float time, Material mat)
    {
    //While we are still visible, remove some of the alpha colour
    while (mat.color.a > 0.0f)
    {
    mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, mat.color.a - (Time.deltaTime / time));
    yield return null;
    }
    }


    IEnumerator FadeIn(float time, Material mat)
    {
    //While we aren't fully visible, add some of the alpha colour
    while (mat.color.a < 1.0f)
    {
    mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, mat.color.a + (Time.deltaTime / time));
    yield return null;
    }
    }


    }
     
  4. Untoldecay

    Untoldecay

    Joined:
    Apr 9, 2016
    Posts:
    24
    Thanks, I don't have much experience but I'll give it a try.

    You have a little button for inserting code "properly" in the post's topbar window.
    You may want to copy & paste your c# there.

    Cheers
     
    StefanK_93 likes this.
  5. StefanK_93

    StefanK_93

    Joined:
    Apr 10, 2019
    Posts:
    6
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SphereChanger : MonoBehaviour {
    6.  
    7.  
    8.  
    9.     //This object should be called 'Fader' and placed over the camera
    10.     GameObject m_Fader;
    11.  
    12.     //This ensures that we don't mash to change spheres
    13.     bool changing = false;
    14.  
    15.  
    16.     void Awake()
    17.     {
    18.  
    19.         //Find the fader object
    20.         m_Fader = GameObject.Find("Fader");
    21.  
    22.         //Check if we found something
    23.         if (m_Fader == null)
    24.             Debug.LogWarning("No Fader object found on camera.");
    25.  
    26.     }
    27.  
    28.  
    29.     public void ChangeSphere(Transform nextSphere)
    30.     {
    31.  
    32.         //Start the fading process
    33.         StartCoroutine(FadeCamera(nextSphere));
    34.  
    35.     }
    36.  
    37.  
    38.     IEnumerator FadeCamera(Transform nextSphere)
    39.     {
    40.  
    41.         //Ensure we have a fader object
    42.         if (m_Fader != null)
    43.         {
    44.             //Fade the Quad object in and wait 0.75 seconds
    45.             StartCoroutine(FadeIn(0.75f, m_Fader.GetComponent<Renderer>().material));
    46.             yield return new WaitForSeconds(0.75f);
    47.  
    48.             //Change the camera position
    49.             Camera.main.transform.parent.position = nextSphere.position;
    50.  
    51.             //Fade the Quad object out
    52.             StartCoroutine(FadeOut(0.75f, m_Fader.GetComponent<Renderer>().material));
    53.             yield return new WaitForSeconds(0.75f);
    54.         }
    55.         else
    56.         {
    57.             //No fader, so just swap the camera position
    58.             Camera.main.transform.parent.position = nextSphere.position;
    59.         }
    60.  
    61.  
    62.     }
    63.  
    64.  
    65.     IEnumerator FadeOut(float time, Material mat)
    66.     {
    67.         //While we are still visible, remove some of the alpha colour
    68.         while (mat.color.a > 0.0f)
    69.         {
    70.             mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, mat.color.a - (Time.deltaTime / time));
    71.             yield return null;
    72.         }
    73.     }
    74.  
    75.  
    76.     IEnumerator FadeIn(float time, Material mat)
    77.     {
    78.         //While we aren't fully visible, add some of the alpha colour
    79.         while (mat.color.a < 1.0f)
    80.         {
    81.             mat.color = new Color(mat.color.r, mat.color.g, mat.color.b, mat.color.a + (Time.deltaTime / time));
    82.             yield return null;
    83.         }
    84.     }
    85.  
    86.  
    87. }
    88.  
     
  6. StefanK_93

    StefanK_93

    Joined:
    Apr 10, 2019
    Posts:
    6

    Did you have some time to take a closer look on this topic?

    Cheers
     
  7. Untoldecay

    Untoldecay

    Joined:
    Apr 9, 2016
    Posts:
    24
    Hello @StefanK_93 , I did not had the time yet.
    I may have some time this week.

    Cheers!
     
  8. Untoldecay

    Untoldecay

    Joined:
    Apr 9, 2016
    Posts:
    24
    It is silly question but does your console displaying any error message?
     
  9. StefanK_93

    StefanK_93

    Joined:
    Apr 10, 2019
    Posts:
    6
    Yes like the ones you can see in the picture attached:
    Yes like the ones you can see in the picture attached:
    upload_2019-4-25_15-52-3.png
     
  10. luminal23

    luminal23

    Joined:
    Sep 4, 2018
    Posts:
    6
    As you can see in the picture there is a warning message that says: No Fader object found on camera.

    As I can see in the code that you posted you are searching the Fader object this way

    m_Fader = GameObject.Find("Fader");


    So I'm assuming that your "Fader" object is not called "Fader" in the hierarchy, is that the case?
     
  11. StefanK_93

    StefanK_93

    Joined:
    Apr 10, 2019
    Posts:
    6
    I renamed the Fader to "Fader". Still doesn´t work.... Following errors occur:

    upload_2019-5-2_17-5-47.png
     
  12. luminal23

    luminal23

    Joined:
    Sep 4, 2018
    Posts:
    6
    Sorry for the late response, you solved the warning I was talking about.

    For the XR error, Download and import the asset VR Samples from the Asset Store