Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 17, 2021 at 07:54 PM by Neooz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Neooz · Feb 16, 2021 at 08:24 PM · collision detectionboxcolliderignorecollision

How to make a trigger box collider SOLID (with physics)?

Hello. I'm trying to make a trigger gameobject that applies physics. I want my bullet to bounce of the target and I want to count how many times I've hit the target to keep score.

At the moment I have two box colliders at the exact same location, one for the trigger, one for the physics. Everything works except the two box colliders always collide once at the start and therefore add score, but i only want to add score if the bullet hits the target. Below is my code so far. After the code I'll explain what I've done so far. My real question is: how can I make a trigger gameobject that is solid (with physics)?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TargetCollision : MonoBehaviour
 {
     [SerializeField] int score;
     private bool hasCollided = false;
     [SerializeField] BoxCollider target;
     [SerializeField] BoxCollider trigger;
 
     private void Start()
     {
         score = 0;
         Physics.IgnoreCollision(target, trigger, true);
         Physics.IgnoreLayerCollision(9, 10);
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (gameObject.CompareTag("Target"))
         {
             if (!hasCollided)
             {
                 hasCollided = true;
                 score = score + 5;
                 Debug.Log("5 points! " + score);
             }
                
         }
     }
 
     private void LateUpdate()
     {
         hasCollided = false;
     }
 }

What I've tried. I've put the two colliders from the shooting target at two different layers and unchecked them in the project settings/physics tab. I've used both lines of code to ignore collisions between objects or layers (as you can see in the code). I've tried to write code that also checks if the "other" is my bullet, but this somehow breaks the score system.

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by mtrest · Feb 17, 2021 at 01:32 AM

@Neooz All you have to do is put a Mesh Collider on the thing that you want to shoot at and put a Rigidbody as well as a Mesh Collider on the bullet. This should work, but contact me if you have any problems!

Comment
Add comment · Show 5 · 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 Neooz · Feb 17, 2021 at 08:23 AM 0
Share

@mtrest thank you for your response! I've tried the solution you've provided, however this didn't fix the problem with the bullet going through sadly.

At the beginning of scripting this I had an issue that the first shot was detected four times, thats why i had the bool inplemented. Now that I've changed the OnTriggerEnter to OnCollisionEnter and checked for "other" tag = projectile, I could apperantly remove the bool and fix the whole issue. Thank you for your time though!

avatar image mtrest Neooz · Feb 17, 2021 at 04:53 PM 0
Share

@Neooz Another option is to copy the GameObject you want to shoot at, scale the Box Collider down slightly, delete the Mesh Renderer, and turn off "Trigger" for the Box Collider. Contact me if you have any problems with this solution!

P.S. Did you put a Box Collider on the bullet?

avatar image Neooz mtrest · Feb 17, 2021 at 06:12 PM 0
Share

@mtrest Yeah, thats exactly what I came up with in the end! This did cause a score problem as those two boxes would "collide" right from the start if I used compareTag to look for the target instead of the bullet, since there are two boxes colliding on top of each other then. After I changed it to look for the bullet as a collider, this problem was fixed. Oh and no, I had a capsule collider on the bullet. I use continuous dynamic detection on the bullet because of that shape. I read the round edge could cause multiple triggers.

The issue has been resolved now. thank you:)

Show more comments

Follow this Question

Answers Answers and Comments

116 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

Related Questions

Finding number of bowling pins knocked over 1 Answer

Box Collider on Spring Joint Randomly Stops Working in Editor and Builds 1 Answer

BoxCollider get stuck when dragging over the border of two BoxColliders 1 Answer

How to make a progress bar go up after two objects collide? 0 Answers

[Not Resolved but Explained] OnTrigger* / OnCollision* Discrete collisions issue 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