0
\$\begingroup\$

I have an interesting problem - I have been looking up for solutions on this but I think there is something I am missing and I require a little more knowledge.

I have a scene and the scene has an Oculus OVRRig set up. I have a script with sphereCast connected to the right hand. This Spherecast shoots out to select a particular layer. This is all working fine.

I have also the setting of an alpha color on the cross hair brings up the object when the SphereCast hits.

enter image description here

It is up until here it all works fine. It is here I reiterate that my object is a spawned prefab - which seems to be causing trouble. I say this because I cannot select and change the material of the crossHair as it affects all the prefabs.

I simply cannot deactivate it once the SphereCast stops pointing at it.

Two Pieces of code. The first sets up the sphere cast - attaches to a specific target and sets up a bool - Switcher - for the next scene to allocate the MeshRenderer for the crosshair.

public float radius;//For the radius of the SphereCast
public LayerMask LayerMask1;//The current Layer I use for focusing on the Raycasting Objects
public float MaxtDistance;// The Overall reach of the Spherecast

public GameObject currentHitObject;
public Vector3 up;
public RaycastHit hit;
public Vector3 startPos;
public int moveSpeed;
public bool TransitionSwitcher = false;
public bool Switcher = false;

void Update()
{
    startPos = transform.position;
    up = transform.forward;

    if (TransitionSwitcher)
    {
        StartCoroutine(MoveTo());
    }

    if (Physics.SphereCast(startPos, radius, up, out hit, MaxtDistance, LayerMask1, QueryTriggerInteraction.UseGlobal))
    {
    //    Transform target = hit.collider.transform.GetChild(2).GetChild(0);
     //   target.GetComponent<MeshRenderer>().enabled = true;
        // OVRInput.SetControllerVibration(0.3f, 0.3f, OVRInput.Controller.RTouch);
        TransitionSwitcher = true;
        Switcher = true;
        currentHitObject = hit.transform.gameObject;
    }
    else
    { 
        OVRInput.SetControllerVibration(0.0f, 0.0f, OVRInput.Controller.RTouch);
        Switcher = false;
    }

}
IEnumerator MoveTo()
{
    hit.collider.gameObject.transform.position = Vector3.Lerp(hit.transform.position, startPos, Time.deltaTime * moveSpeed);
    TransitionSwitcher = false;
    yield return null;
}

This second piece of code takes the Bool Switcher from the code above and either switches on or off the renderer. I know why it isn't working properly in this - it allocates the boolean to all the prefabs this is attached to.

public GameObject cube;
public GameObject crossHair;
public bool result = false;

// Use this for initialization
void Start()
{
    cube = GameObject.FindWithTag("Player");
    crossHair.GetComponent<MeshRenderer>().enabled = false;
}

void Update()
{
    bool result = cube.GetComponent<Sphere2>().Switcher;
    print(result);

    if (result == true)
    {
        print("on");
        OVRInput.SetControllerVibration(0.3f, 0.3f, OVRInput.Controller.RTouch);
        crossHair.gameObject.GetComponent<MeshRenderer>().enabled = true;
    }
    else if (result == false)
    {
        print("off");
        OVRInput.SetControllerVibration(0.0f, 0.0f, OVRInput.Controller.RTouch);
        crossHair.gameObject.GetComponent<MeshRenderer>().enabled = false;
    }
}

Can anyone point me in the right direction with this? It doesn't have to be the solution handed to me - rather how do I turn specific meshrenderers on and off when my spherecast hits them and leaves their reach respectively?

\$\endgroup\$
0
\$\begingroup\$

I fixed it in a Eureka but feeling dumb moment. So maybe this will be useful to someone out there - so they don't spend an entire day on it too.

I used a temp variable. Boy don't I feel silly.

Anyways here's the code (cut down a little):

    public Transform TempTarget;

void Update()
{
    startPos = transform.position;
    up = transform.forward;

    if (TransitionSwitcher)
    {
        StartCoroutine(MoveTo());
    }

    if (Physics.SphereCast(startPos, radius, up, out hit, MaxtDistance, LayerMask1, QueryTriggerInteraction.UseGlobal))
    {
      Transform target = hit.collider.transform.GetChild(2).GetChild(0);
      TempTarget = target;
        target.GetComponent<MeshRenderer>().enabled = true;
        TransitionSwitcher = true;
        currentHitObject = hit.transform.gameObject;
    }
    else
    { 
        OVRInput.SetControllerVibration(0.0f, 0.0f, OVRInput.Controller.RTouch);
        TempTarget.GetComponent<MeshRenderer>().enabled = false;//
       // Switcher = false;
    }

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.