- Home /
Is Object At Location?
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
your build is crashing for me. Also, what currently happens with your existing script, anything at all?
webplayer should work now.. corrupt upload fixed (press y for edit mode, not much there yet).. currently i am not even getting triggers from the collision, maybe because it is being instantiated already inside the collider it doesnt know its hitting anything? no debug logs co$$anonymous$$g back.. Also I am trying with mesh colliders and read must use convex which i am.
as I understand it, mesh collider to mesh collider isn't supported - it might be better to use a simplified collider for placement purposes (as you won't really need the accuracy at that point).
Answer by dju4ic · Jun 11, 2011 at 02:32 AM
OK, I have come across a solution, very simple, I had to work around and use a different method, but simple enough, I am using a group of objects with a transparent cube that fills the whole grid space, collider is at .9 scale and constrained all on the rigidbody, then added the basic script:
public class EditCollider : MonoBehaviour {
private Color _tmpColor;
private int _isColliding;
void Start () {
_tmpColor = this.gameObject.renderer.material.color;
}
void Update() {
CheckCollision();
}
void OnCollisionEnter(Collision collision) {
_isColliding = 1;
}
void OnCollisionExit(Collision collision) {
_isColliding = 0;
}
public void CheckCollision() {
if (_isColliding == 1){
this.gameObject.renderer.material.color = new Color(255, 0, 0, .5f);
}
else {
this.gameObject.renderer.material.color = _tmpColor;
this.gameObject.renderer.material.color = _tmpColor;
}
}
}
I also updated the Web build to show the results.
Answer by Chris D · Jun 10, 2011 at 08:59 PM
put colliders back on your objects
in your update, check for collisions with existing objects
if collision is detected, change your texture/shader (check out Material) and disallow placement
if your code isn't working, post it here so we can pick at it :D
Answer by Eric5h5 · Jun 10, 2011 at 09:01 PM
Since you're using a grid system, generally you'd represent the map internally with an array. So you can check if the array location is already occupied.
well, its very rudimentary - basically takes location on terrain and averages location to instantiate every 4 units.
Your answer
Follow this Question
Related Questions
Use ray cast against objects but avoid colliding against other objects 2 Answers
Raycast/Non-Physics Collider Discrepancy 0 Answers
Extend the Collider When Picking Up Objects 0 Answers
Raycast and Hit Problem 0 Answers
Mouse clicking on an object (Raycast.hit) for another object to connect to it? 0 Answers