- Home /
Prevent shooting when gun is inside wall
Hi
The Guns in my game do not have a collider on them. They have CapsuleCollider with isTrigger = true. This is on purpose because of the desired character movement I want to achieve on the character (only the player's capsule collider is used to prevent going through walls) So, as a result the gun can enter the walls and shoot through. To prevent shooting when the gun's inside a wall, I did this : The gun has a script : GunCollissionHandler Since the gun has a CapsuleCollider Trigger, I do this: Whenever gun enters a wall,
OnTriggerEnter(Collider other){
if(other.layer = "Wall"){
isInsideWall = true;
}
}
OnTriggerExit(Collider other){
if(other.layer = "Wall"){
isInsideWall = false;
}
}
Then when shoot button is pressed, obviously I shoot when isInsideWall is false.
But this has a problem (at the corners of the room apparently). When character rotates, the gun moves from inside one wall to another. It's isInsideWall becomes wall and It can shoot, even though it is inside another wall. I even tried counting the colliders, like this :
OnTriggerEnter(Collider other){
if(other.layer = "Wall"){
numWallsInside++;
}
}
OnTriggerExit(Collider other){
if(other.layer = "Wall"){
numWallsInside--;
}
}
Then, when shooting, check if (numWallsInside < 1). This also fails in some cases of death and respawn. I hope you understand the problem.
Thanks.
Just reset the counter on death? Your idea should work, even though I feel like it might be prone to errors. But well, I don't really have better idea to give you.This also fails in some cases of death and respawn.
Good idea, but in the development process I just used OnTriggerStay in this part for now, and let this thing stay for a while, while(otherfeaturesdevelop). Will come back to this and share if I found a better solution.
Answer by MathewAlden · Mar 31, 2017 at 04:39 AM
Okay, I'm kinda new to this but here's what I'm thinking.
First of all, most games make the player's collider large enough that the gun won't go into the wall in the first place. Will this not work with your game?
Second, if you're sure that your guns can pass through walls, then don't try to constantly track whether your gun is currently inside a wall. Too difficult, right? Rather, quick caste a ray from your head to your gun. If the ray hits a wall before it hits your gun, then you know your gun is in a wall.
Just a though... I haven't tested it myself.
Depends on the games, but it actually is pretty common for gun to go through walls as in many cases it would be strange to get stuck if guns barrel hits something especially with long weapons like sniper rifles.
Raycasting would probably work, but I don't really like the idea as raycasting is pretty slow thing to do and you would need to do it constantly.
Okay, I know raycasting is expensive, but is like one 1 meter raycaste per frame too much? It's not like you're shooting more than one or two bullets per frame.
I'm honestly curious. I'm still learning this engine.
It is short and you can make it ignore all the other layers except walls, but I still it is advisable to avoid using raycasts when other methods are available. You probably aren't even shooting one bullet per frame as long there is only one gun that you need to keep checking.
When your game is simple and target device isn't mobile or browser it might not have any impact, but if you keep making your games with the $$anonymous$$d set "it still is running just fine" you might eventually end up in situation where you are ton of stuff to change because it happened to be easy way to deal with something.
I'm not really saying that you absolutely should not use it, I'm saying that you should try to think of some other good working way to complete it and if you cant then go for it. Raycasting is a great tool for multiple purposes, but should be avoided if there is cheaper way to do same thing.
Your answer
Follow this Question
Related Questions
Make Ray hit own collider 1 Answer
How to design an Archer Board Scoring System 1 Answer
Trigger to some, Collider to others 1 Answer