Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SanjayBhambhu · Mar 31, 2017 at 04:18 AM · collisionphysicsraycastcollidertrigger

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kossuranta · Mar 31, 2017 at 10:56 AM 1
Share

This also fails in some cases of death and respawn.

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.
avatar image SanjayBhambhu Kossuranta · Mar 31, 2017 at 04:37 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kossuranta · Mar 31, 2017 at 10:53 AM 0
Share

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.

avatar image MathewAlden Kossuranta · Mar 31, 2017 at 04:20 PM 0
Share

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.

avatar image Kossuranta MathewAlden · Mar 31, 2017 at 04:33 PM 2
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Compound collider object behaving as a whole? 1 Answer

Particular collider not working properly... 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges