I have been looking for a while for a solution to my current problem and am wondering if raycasting or colliders would be the best approach.
Here is my scenario - I am currently working on a 3d map editor via raycasting and a gridding system. I am using transparent prefabs that ignore raycast to show the currently selected object to create, then clicking in location creates the object.
I now need to check if there is already an object at this location, if there is, I need to turn the transparent material color red (showing that you can not build here).
Currently I have no code to detect if there is an object currently at this location and not exactly sure where to start. I am working all with C# and really, the event that is triggered doesn’t matter, just a debug statement even saying there is a collision with an object already here would be fine.
I have experimented with colliders with no luck: instantiate and if collider is triggered perform function not working. So decided to wipe the slate clean on this issue and start with some more professional advice on how to tackle it. Thanks.
edit: Code update
I have been attempting with OnCollisionEnter within a script on the object itself.
CODE:
public class EditCollider : MonoBehaviour {
public Color _tmpColor;
public GameObject _tmpObj;
public int _isColliding;
void Start () {
_tmpObj = this.gameObject;
_tmpColor = this.gameObject.renderer.material.color;
}
void OnCollisionEnter(Collision collision) {
_isColliding = 1;
_tmpObj.renderer.material.color = Color.red;
Debug.Log("Is Colliding: " + _isColliding);
}
void OnCollisionExit(Collision collision) {
_isColliding = 0;
if(!_tmpObj)
return;
// Unset raycast targets material color
_tmpObj.renderer.material.color = _tmpColor;
Debug.Log("Is Colliding: " + _isColliding);
}
}
edit: web build location: http://crystalvortex.ca/Unity/WebPlayer.html