How would i make this script color the object the rays touch?
function Update () {
var dwn = transform.TransformDirection (Vector3(0, -1, 0));
if (Physics.Raycast (transform.position, dwn, 10)) {
print ("There is something below the object!");
}
}
How would i make this script color the object the rays touch?
function Update () {
var dwn = transform.TransformDirection (Vector3(0, -1, 0));
if (Physics.Raycast (transform.position, dwn, 10)) {
print ("There is something below the object!");
}
}
Hello there!
That depends a bit on what shader you’re using.
If it’s the standard Diffuse then this could should work:
function Update () {
var dwn = transform.TransformDirection (Vector3(0, -1, 0));
var hit = new RaycastHit();
if (Physics.Raycast (transform.position, dwn, hit, 10)) {
hit.collider.gameObject.renderer.material.color = Color.red;
}
}
thanks!
How do i make it to where the rays are cast every second?
I’m not exactly sure what you mean, but if you want to cast only one ray a second you’ll need a timer.
Something along these lines:
var timer = 0.0f;
function Update () {
timer -= Time.deltaTime;
if(timer <= 0)
{
timer = 1.0f;
var dwn = transform.TransformDirection (Vector3(0, -1, 0));
var hit = new RaycastHit();
if (Physics.Raycast (transform.position, dwn, hit, 10)) {
hit.collider.gameObject.renderer.material.color = Color.red;
}
}
}
alternatively you could use a coroutine
void Start(){
StartCoroutine("RayRoutine");
}
IEnumerator RayRoutine(){
while (true){
// DO RAYCAST
yield return new WaitForSeconds(1.0f);
}
}