I’m making an RTS game where you build towers, and I want to not let the player build a tower when the position he will build the tower already has a tower. I added a collision box and layers for this, but it seems like I need a rigidbody to trigger the collisions… and adding rigidbody messes it up because when the towers collide they just throw away eachother into space.
How can I implement collisions using collision boxes components without a rigidbody?
You could do a raycast where you want to place the new tower.
Or you could have a rigidbody on the new tower that you want to place, and then it will register collisions with existing towers, and then destroy the rigidbody after it’s successfully placed in position.
Thanks, raycasting works!
but how can I make raycast not check for collision with itself? I mean it’s always checking a collision with itself
here’s my codes:
void CheckCollisions(){
Vector3 pos = Vector3.zero;
RaycastHit hit;
collidingwithothertower = true;
if (Physics.Raycast (transform.position+Vector3.up,-Vector3.up,out hit,5.0f)) {
if (hit.transform.position != transform.position) {//don't collide if they are both the same object
collidingwithothertower = true;
Debug.Log ("Collision found");
}
}
}
tryed adding the if (hit.transform.position != transform.position) {//don’t collide if they are both the same object
but it now doesn’t detect any collisions
You could have new towers on the IgnoreRaycast layer, and once placed change them to a different layer.
I searched around and found that I can detect all collisions instead of stopping at the first one:
void CheckCollisions(){
Vector3 pos = Vector3.zero;
RaycastHit[] hits;
collidingwithothertower = false;
hits = Physics.RaycastAll (transform.position+Vector3.up*2,-Vector3.up,5.0f);
foreach(var hit in hits){
if(hit.transform.position != transform.position){
collidingwithothertower = true;
Debug.Log ("Collision found");
Debug.Log (hit.transform.name);
}
}
}
thanks for the help!