- Home /
changing objects color through a raycast?
Is this possible?
My script changes the color of my player but I'd like it to change the color of anything hit that has a tag of "placedObject" also a few seconds afterwards to change the color back to normal. Could anyone help me with this? thanks.
function Update (){
if(Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 10.0)){
if(hit.collider.tag == "placedObject"){
var renderer : Renderer = hit.collider.renderer;
renderer.material.color = Color.yellow;
print ("hitObject");
}
}
}
}
You need to specify what you have attempted and what isn't working in your script.
Looking at your code, the color changes on the object the script is attached to. Access the renderer of the transform of the hit object to alter its color.
Yeah that's what I meant sorry haha, I was looking for help with scripting this could you give me hand?
hit.collider has a property called renderer, this is the renderer of the gameobject you hit, change that, you already check the gameobjects tag.
second, use waitforseconds and yield out until you want to complete the function(i.e. change the color back), make sure you have a copy/reference to the original color.
Okay I've figured the first part out cheers, as for Yield witforseconds does this go in the Update function?
You create a coroutine for this yield waitforseconds thing.
Answer by Lo0NuhtiK · Jul 07, 2014 at 02:21 PM
function Update()
{
//.......
if(hit.collider.tag == "placedObject")
{
Wait(hit.collider.renderer) ;
}
}
function Wait(rend : Renderer)
{
var originalColor = rend.material.color ;
rend.material.color = Color.yellow ;
yield WaitForSeconds(2) ;
rend.material.color = originalColor ;
}
Your answer
Follow this Question
Related Questions
help with game error 0 Answers
I want to click on my screen and make my character move there, having trouble with raycasts. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Drag Rigid Body No Distance Limit and how to add Mouselock to a scrip 1 Answer
Destroy only selected object 1 Answer