- Home /
Using Raycasting to identify clickable object and to activate skybox change
Hello (noob alert)
I have a scene in which the viewer's gaze is supposed to see a color change in a clickable object. The viewer can then click the object to trigger a change in skybox. I already have the skybox set up with two different materials. I have scripts for both that run ok independently - that is, a script that can get the reticle to change skybox material with a click and a script that can change the color of the object. I am trying to get both scripts into one so they can work together. Here's the script that I'm using for the raycast color change:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class CameraRaycast : MonoBehaviour {
public MeshRenderer rend; public bool hovering; public bool hoveringOld;
void Start () {
//rend = GameObject.Find("Earth Collider").GetComponent<MeshRenderer>();
}
// Update is called once per frame
void Update () {
Ray ray;
RaycastHit hit;
ray = new Ray(this.transform.position, this.transform.rotation * Vector3.forward);
if (Physics.Raycast(ray, out hit)&& hit.transform.tag == "Clickable"){
hovering = true;
Debug.Log("Clickable object");
if(Input.GetMouseButtonDown(0)){
//SceneManager.LoadScene("Skybox")
print ("Load Scene");}
}
else{
hovering = false;
}
if (hovering != hoveringOld){
if(hovering == true){
rend.material.color = Color.red;
}
if(!hovering){
rend.material.color = Color.clear;
}
}
hoveringOld = hovering;
}
}
And here's the script for the skybox material change: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class CameraRaycast : MonoBehaviour {
public MeshRenderer rend; public bool hovering; public bool hoveringOld;
void Start () {
//rend = GameObject.Find("Earth Collider").GetComponent<MeshRenderer>();
}
// Update is called once per frame
void Update () {
Ray ray;
RaycastHit hit;
ray = new Ray(this.transform.position, this.transform.rotation * Vector3.forward);
if (Physics.Raycast(ray, out hit)&& hit.transform.tag == "Clickable"){
hovering = true;
Debug.Log("Clickable object");
if(Input.GetMouseButtonDown(0)){
//SceneManager.LoadScene("Skybox")
print ("Load Scene");}
}
else{
hovering = false;
}
if (hovering != hoveringOld){
if(hovering == true){
rend.material.color = Color.red;
}
if(!hovering){
rend.material.color = Color.clear;
}
}
hoveringOld = hovering;
}
}
Thanks for any help.
Your answer
![](https://koobas.hobune.stream/wayback/20220612104957im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
unity 3.4 crashes when i get to a certain point in the character generator script? 0 Answers
How to create a UnityScript array and access the data in each cell. 1 Answer
Serialize is working in debug, but it has no effect 0 Answers
Changing a UI image sprite in scripts results in a black texture 0 Answers
Swapping animation clips at runtime 1 Answer