Very strange trigger behavior in Unity2D (4.5) - c#

I am developing 2d platformer and I've faced with a very strange triggers behaviour in Unity. I have made the enemy that can throw objects into main player. So I have made two empty GameObjects with Circle2D colliders on them and attached them into the enemy GameObject. The first trigger ensures that when enemy hand will pass through it, it will spawn an object that will be thrown in future, and the second trigger is responsible for the throw that object into player. The first trigger ensures that when the enemy's hand will pass through it, it will spawn an object that will be thrown in future, and the second trigger is responsible for throwing an object into main player. Every trigger has a Rigidbody2D component attached to it. The hands of the enemy have tags "right hand" and "left hand" and I am checking the in the OnTriggerEnter2D method. This method is calling and I have log messages and this method is detecting that left or right hand are entering trigger. But the problem is in that this method is calling when the hand has already passed through trigger and get out of its bounds. Can anyone help me and explain why this is happening? I have attached a picture where I draw a scheme and described my problem.

Related

Unity C# capsule collider and rigid body don't trigger using transform.position to move

I have 2 simple capsules. 1 is stationary and the other is moving to the same tile on the tilemap using transform.position.
Both capsules have capsule colliders and rigid bodies. I've attempted to remove the rigid body but from what I can tell, the OnCollisionEnter function requires a rigid body to work.
My script, attached to both of these, is a simple:
private void OnCollisionEnter(Collision other)
{
print("Collision Detected! " + other.gameObject.name);
}
I've used combinations of 'isKinematic', 'isTrigger', placing one component before the other then reversing, ensuring I have the script attached to both objects, making sure my capsule collider is sized to the object, and looked at other people's issues to try their steps to fix my issue.
Nothing seems to trigger the collision between the 2 capsules. It's got to be something small I'm missing.
I have a tag on both objects and have tried things like if(other.gameObject.tag == "myTag") {...} but that's not working for me, either.
Can anyone spot my mistake? Let me know if you need to see anything else - happy to provide images or whatever will help. Thanks in advance!
Above is the basic identical hierarchy of both capsules with components attached to the capsule mesh. Both have the parent empty objects they're stationed under. (Which themselves, contain scripts but no collider or rigid body)
Here is an infographic to show when a collision message will be detected by OnCollisionEnter between two objects. Both objects will need some sort of collider, and will most likely need a Rigidbody.
You will not want to set isTrigger as that will not make it physically react to a collision, but will just detect when a collision occurs. It will also not call OnCollisionEnter but will call OnTriggerEnter. Setting both not as triggers, adding a collider, and giving them Rigidbodies should allow the collision to be detected. You will also need to attach this script to one of the objects that have the collider. Are there other components on the objects you are using?
Make sure you are not looking for
void OnTriggerEnter(Collision collision){}

Networked AR Unity can't make a bullet a child of the ground plane stage

I am trying to make an AR game and am currently using Unity's high-level networking classes. I have set my player prefab to be spawned in one of the two network spawn locations, which are both childs of the Ground Plane Stage. When user has tapped to make their ground plane stage and has tapped to be the host, their player character appears. Unfortunately, if they press the fire button, the bullets appear above the stage and unscaled, meaning they aren't parented to the stage. This confuses me because I've checked many times and the bullet emitter is a child of the player, and in my code it references said emitter. Thus I'm rather confused why the bullets don't seem parented.
I've attempted to attach a script to make the bullet emitter a child of the player when it spawns. I've also tried making it a child of the stage when it spawns. I've tried making the player character not dependent on the Network manager spawning it when the player joins, but then that leads to other networking problems when it comes to controlling the character, but it can shoot then.
The only one that was relatively successful was making the bullet a child of the stage when it spawned, but it would only stay in one place. Attempting to make the bullet a child of the player did nothing
//This is the class I'm trying to use to make the object a child of
something
public class AddToBeetle : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameObject player =
GameObject.FindGameObjectWithTag("Player");
transform.SetParent(beetle.transform, false);
}
}
It rarely prints any error messages. I hope that I can eventually get the bullet to spawn in front of the player model when the button is pressed.
Oh gosh I was dumb, the one thing I didn't apply the script to was the player's bullet emitter. It was super late and that's probably why I missed trying it on that. Anyway I hope this helps someone else if they ever have to deal with AR and Unity.

(Unity3D) Is it possible to call OnTrigerCollider2D() without binding any script to the Collider2D directly?

I want to use a single script to control all elements of one scene. Let's call it "Director" script, bound to EventSystem.
I knew that I can get a GameObject by name or tag, and then manipulated the components such as transform. Thus I wondered if I can define the OnTrigerCollider2D() in this "Director” script without binding any script to the Collider2D GameObject, just like I define the value of the transform? If possible, how?
According to some friend, it is necessary to bind at least a basic script to the Collider2D to authorize the "Director" script to get access to the Collider2D event, no way to bypass this limitation?
From the docs:
MonoBehaviour.OnTriggerEnter2D(Collider2D)
Sent when another object enters a trigger collider attached to this
object (2D physics only).
Further information about the other collider is reported in the
Collider2D parameter passed during the call.
In order for the event to fire, the trigger collider must be attached to the same object as your behavior. Using a small script on the object with the collider with a reference to call a method on your "Director" is the most common way I've seen this done. As far as I know, it's impossible for objects to "subscribe" to collision events fired by other objects.
A poster here suggests that one solution could be to create a collision manager that the "Director" checks, but the objects actually having collision events would need to notify the collision manager, so with that solution you're just adding a step. Depending on what you actually want to do with collision events, it might make sense for you though.

OnTriggerEnter being called on unexpected object

Maybe I have a misunderstanding as to how OnTriggerEnter is supposed to work but here is my situation and how it differs from what I would expect.
I've created a very simple test project with a single scene. I have three objects in my scene - a player, an enemy, and an attack. The enemy and player both have rigid bodies and box colliders, neither of which is a trigger. The attack object has a box collider that IS a trigger. For scripts, the player has a script with nothing in it other than an OnTriggerEnter function that logs info to the console. The enemy has a simple script that just enables and disables the attack object in a cyclic pattern. When the attack is enabled it collides with the player.
Github with cloneable project/code: https://github.com/valevalorin/TriggerTest
What I would expect to happen:
The attack object is enabled and collides with the player. No console output is logged. The player object does not have a trigger collider on it so it's OnTriggerEnter should never be called.
What actually happens:
When the attack object collides with the player, the player's OnTriggerEnter function gets called and output is logged to the console.
As far as I can tell, OnTriggerEnter should only be called on objects that actually have a trigger associated to them. Is this not how OnTriggerEnter works?
From the documentation:
Description
OnTriggerEnter is called when the Collider other enters the trigger.
This message is sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger.
Emphasis mine.

Somehow physically colliding with Collision Triggers

With a whole bunch of copy-pasting from all over the web, I managed to hack together a piece of C# code which spawns a random object from an array when the player enters a large, spherical collision trigger. When the player leaves the trigger, the object is deleted. It's working almost as intended, only problem is that six out of ten times when the player passes through the collider, for a split second it feels like a physical collider. Meaning that although the player is trying to move straight forward, he or she will move left or right along the trigger collider for about half a second, before the object spawns and the player can move freely again.
I was under the impression that checking the "Is Trigger" box would remove all physical properties of that specific collider? Am I missing something?
I'm using the standard FPS Character Controller in Unity 5 if that might have something to do with it.
All help is greatly appreciated.
Make sure the spherical collision trigger has a rigid body component, and that the "Is Kinematic" property of the rigid body is checked.
Realized my solution didn't get posted as I thought it had. Yoyo's solution was correct. The object containing the code was also the trigger. Switched so that the player was the trigger and now all is well.

Categories