Jump to content

Unity Collision between triggers

HeartOfGermany

Hello to everyone.

 

I am having trouble in finding a way, to detect the position, where 2 Objects collided.

What I have:

 

Bullet with Rigidbody2D and CapsulleCollider as trigger

Target with Rigidbody2D and PolygonCollider as trigger

 

It seems, this would work nice without using the Colliders as triggers, but I can not allow the objects to react to physics outside of my code.

 

    private void OnTriggerEnter2D(Collider2D collisionData)
    {
      //Debug.Log(collisionData.collision.position);
      Debug.Log("Here it is: " + collisionData.gameObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position));
    }

 

 

 

The problem is: I get the error: "MissingComponenteException: There is no 'Collider' attached to the "Asteroid2" game object, but a script is trying to acces it."

 

Ah thanks Unity. I know I want to acces it. But it is not working. Why not, it has a Collider?

 

What is not a good idea:
- Raycast > Since I realy want the exact position respecting the shape of projectile and target (the Colliders I chose in the first place)

- set "Is Trigger" to false on the collider of the target (disabling also gives the same error anyway)

 

What I realy need: The hit point or hit points of the collision. How do I archive that?

 

Thanks for help.

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

44 minutes ago, HeartOfGermany said:

 


    private void OnTriggerEnter2D(Collider2D collisionData)
    {
      //Debug.Log(collisionData.collision.position);
      Debug.Log("Here it is: " + collisionData.gameObject.GetComponent<Collider>().ClosestPointOnBounds(transform.position));
    }

 

First step :
Start the game, get to a collision where the issue happens, then pause it.
Go in the editor and click on the object and see if the collider is there.

Is it instanced ? does the source of the instance have a collider set as trigger ?
Is part of the script somewhere maybe disabling that trigger (or removing it altogether) beforehand ?

what do you need the specific point of collision for ?

~New~  BoomBerryPi project !  ~New~


new build log : http://linustechtips.com/main/topic/533392-build-log-the-scrap-simulator-x/?p=7078757 (5 screen flight sim for 620$ CAD)LTT Web Challenge is back ! go here  :  http://linustechtips.com/main/topic/448184-ltt-web-challenge-3-v21/#entry601004

Link to comment
Share on other sites

Link to post
Share on other sites

Collision in unity:

Collider & Collider detected

Collider & trigger detected

Trigger & trigger NOT detected

 

Edit:

You need to add the collider component to the gameobject(s) with the scripts requiring a collision.

 

Edit edit:
I didn't really read your entire post before posting.

OnTriggerEnter & OnCollision are different methods.

Link to comment
Share on other sites

Link to post
Share on other sites

Why I need that precision?

 

Well, it looks very dumb, if I want to spawn animations at that point.

Also, I want some physics to be applied in a precise way. That absolutely requires the correct position. I want to let the asteroid spin and/or move away from the impact (0 Gravity environment).

Depending on the hit location and the resulting angle AngleA between line(asteroid.midpoint, hitpoint) and line(bulletspawnpoint, hitpoint) the asteroid should start to spin in some way. The bigger the angle AngleA is, the more of the kinetic energy should get transformed into rotation depending on the mass and distanance(hitpoint, asteroid.midpoint). If the Angle is 180°, the energy should only transform into linear motion.

 

Also the impact is supposed to create preasure and depending on the shape of the entity the energy reflects in different ways - which either results in spin, linear motion or a combination of both.

 

Why be so crazy about it?

- It is not a REAL game. I am trying to work on my Unity and C sharp skills. Precisely understanding the relations between all this is important for later projects, that actually should work properly.

 

Long story short (as always):

 

So is there a way, to get a collision position, that actually is correct without using any rayCast method? With rayCast it will be much, much worse in precision, which I'd love to avoid. And using a collider with "isTrigger=false;" will not work, since it would react to other non-trigger colliders, which is also not wanted. Why? The problem is, realism. Standard collider with rigidbody2D will not correctly react to torsion forces. Just imagine a ball hitting another ball on the side with high speed. Some of the friction will induce a spin on the other ball. This is not working with basic unity physics.

 

 

P.S.: yeah, I know I have to code every interaction with other objects from scratch by avoiding the otherwhise usable physics of Unity2D. But that is intended.

First: Old console games (SNES, GBA...) do it all by scratch and rather use the same aproach I want to use (only mine in a refined way).

Second: After getting all the physics working properly I think physics training in Unity is complete to me. Nothing to learn in that direction. (Only thousands of other topics to master left. :D )

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, HeartOfGermany said:

snip

Wut?

 

Can you simplify your question. I'm really confused.

 

What is your problem? What isn't working?

 

You gave me a whole lot of info dump onto what you may be trying to do.

From what I gather in your OP, you have 2 objects that need to collide. Both are triggers, so no physics will be recognized. You also seem to have forgotten to add the collider component to at least 1 object.

 

In your info dump post, you seem to be saying "I need the bullet to interact with the target & I need the bullet to make the target fall over." The physics engine would already do that as that's what it's for.

Link to comment
Share on other sites

Link to post
Share on other sites

Okay clarification:

 

I do not like the physics engine. I want to basically build my own from scratch. This is the main goal. (For both learning and improvement reasons)

Also, the built in physics engine is not accurate in my usecase.

Also, I want some special stuff to happen on impact (sounds much simpler)

 

Both objects have a Collider and a RigidBody2D. Both colliders are triggers. And yes, that is absolutely intentional.

 

 

 

If no purely trigger relatet way is possible, I will try the following:

 

multiple raycasts (360° [in ~5° to 15° steps] from the start, mid and end of the bullet), to get the closest point.

 

But I would love, if there is a simpler, more integrated way to detect trigger collision position (just use an already existing variable). I do not understand, why this is not possible in an obvious way. I read multiple threads where the solution was to invert the "isTrigger=true". It is not intended, to use unity physics for that. This is no solution for me.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×