- Home /
shooting raycast from each triangle on mesh and change its texture
I dont know it this has been answers, did some searching and came out dry.
Question is that, If I was to have a object with a mesh that contains 200 triangles which make up 100 1x1m squares. I want to change the texture of the squares if an object is directly above that square to indicate where that block above is going to land. Currently I have 100 individual cubes with an attached script that does that by each shooting a ray cast in the air and if it hits a certain object with correct tag it changes it texture. here is the script for further understanding:
using UnityEngine;
using System.Collections;
public class TextureChange : MonoBehaviour {
//Texture to change to if ray cast its control block
public Texture colourChange;
//Its orginal texture to revert back to
public Texture previousTexture;
// Update is called once per frame
void Update () {
//variable to contain all layers but layer 8
LayerMask layerMask = 1 << 8;
layerMask = ~layerMask;
//variable to store raycast values
RaycastHit hit;
//if the raycast hit something check objects tag, else keep orignal texture
if(Physics.Raycast(transform.position, Vector3.up, out hit, Mathf.Infinity, layerMask.value)) {
//if that object has tag control block, change the texture to the colourChange texture
if(hit.collider.gameObject.tag == "ControlBlock") {
//change texture to requested texture variable
gameObject.renderer.material.mainTexture = colourChange;
}
//if its an no controlable block keep it as orginal texture
if(hit.collider.gameObject.tag == "UnControlBlock") {
gameObject.renderer.material.mainTexture = previousTexture;
}
} else {
gameObject.renderer.material.mainTexture = previousTexture;
}
}
}
But having 100 blocks seems wasteful if there is an alternative method. Thankyou if for any answers if there is any on this.
Comment