- Home /
The question is answered, right answer was accepted
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.
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!
@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!
@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?
@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:)
Follow this Question
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