Group Group Group Group Group Group Group Group Group

Changing wall color when raycast hit vive



How to change the wall color using raycast hit method in vive controller
when I press the trigger button in the vive controller the color needs to change how to do ? here I am using Player Prefab…

sample code for color change :

public class WallColorChange : MonoBehaviour {

	public Material[] materials;//Allows input of material colors in a set size of array;
	public Renderer Rend; //What are we rendering? Input object(Sphere,Cylinder,...) to render.

	private int index = 1;//Initialize at 1, otherwise you have to press the ball twice to change colors at first.

	// Use this for initialization
	void Start () {
		Rend = GetComponent<Renderer> ();//Gives functionality for the renderer
		Rend.enabled = true;//Makes the rendered 3d object visable if enabled;
void Update()
	void OnMouseDown()

		if (materials.Length == 0)//If there are no materials nothing happens.

		if (Input.GetMouseButtonDown (0))

			index += 1;//When mouse is pressed down we increment up to the next index location

			if (index == materials.Length + 1)//When it reaches the end of the materials it starts over.
				index = 1;

			print (index);//used for debugging

			Rend.sharedMaterial = materials [index - 1]; //This sets the material color values inside the index

Thanks in advance


Do you already have the code to get the collider of the GameObject hit by the raycast?

If so, the most performant way is to add this to the top of the script first:

public Material coloredMaterial;

Now assign the colored material you want to use in the editor.
Next, right after the code that casts the ray and gets the collider (let’s say it’s named hitCollider):

hitCollider.GetComponent<Renderer>.material = coloredMaterial;

If you have no idea where to start, check out the HTC Vive tutorials I wrote:


closed #3

This topic was automatically closed after 166 days. New replies are no longer allowed.