Create Unity classes and methods easily.
Features
- A Rigidbody2D just tells a GameObject how to interact with the physics engine. It doesn't provide a collider for collision. From the Unity docs for RigidBody2D By adding the appropriate collider component, the sprite will also respond to collisions with other sprites.
- The following two script examples create an OnTriggerEnter2D demo. Example1 generates a Unity logo sprite, GameObject1. This sprite is collided with by the Example2 sprite, GameObject2. The Example1 script creates the Rigidbody2D. The kinematic mode is used on this script. Example2 supports the OnCollisionEnter2D method.
- According to the Unity documentation for OnTriggerEnter2D the trigger is ' sent when another object enters a trigger collider attached to this object (2D physics only)'. The function should only be called once per entrance. You could gather further data by logging the number of calls to OnTriggerEnter2D.
How To Use Ontriggerenter2d Unity
All Unity code snippets you need. This extension intends to be the complete collection of Unity snippets for Visual Studio Code.
OnTriggerEnter2D often not called I have a lot of collisions in my game and most of them worked well at first. But I'm noticing a lot of incidents now, where either the Player, an enemy or a projectile just moves throught an object without interacting with it. Here's the script of an enemy for example.
It takes advantage of latest Visual Studio Code snippets features to create the code faster for you.
MonoBehaviour
Create game classes like MonoBehaviours
, NetworkBehaviours
and StateMachineBehaviours
easily. Also create common methods like Start()
, Update()
or OnTriggerEnter2D()
and log calls.
Editor
Create an Editor classes like Editor
, EditorWindow
and PropertyDrawer
as easy as it can be.
ScriptableObject
You never remember the property that goes with the ScriptableObject
to create it via Unity create menu? Not a problem.
Instalation
As in any Visual Studio Code Extension you have several options to install:
- Enter the Visual Studio Code Marketplace, search for Unity Code Snippets (or enter directly on the extension page) and click on Install button.
- Inside Visual Studio Code, enter in the Extensios panel, search for Unity Code Snippets and click on Install button
- Run the following command in the Command Palette:
All the snippets
Start typing the names to create the corresponding snippets.
Game classes:
MonoBehaviour
StateMachineBehaviour
NetworkBehaviour
ScriptableObject
Editor Classes:
Editor
Editor with Reorderable List
(NEW)EditorWindow
PropertyDrawer
ScriptableWizard
MonoBehaviour Methods:
Awake()
FixedUpdate()
LateUpdate()
OnAnimatorIK()
OnAnimatorMove()
OnApplicationFocus()
OnApplicationPause()
OnApplicationQuit()
OnAudioFilterRead()
OnBecameInvisible()
OnBecameVisible()
OnCollisionEnter()
OnCollisionEnter2D()
OnCollisionExit()
OnCollisionExit2D()
OnCollisionStay()
OnCollisionStay2D()
OnConnectedToServer()
OnControllerColliderHit()
OnDestroy()
OnDisable()
OnDisconnectedFromServer()
OnDrawGizmos()
OnDrawGizmosSelected()
OnEnable()
OnFailedToConnect()
OnFailedToConnectToMasterServer()
OnGUI()
OnJointBreak()
OnJointBreak2D()
OnMasterServerEvent()
OnMouseDown()
OnMouseDrag()
OnMouseEnter()
OnMouseExit()
OnMouseOver()
OnMouseUp()
OnMouseUpAsButton()
OnNetworkInstantiate()
OnParticleCollision()
OnParticleTrigger()
OnPlayerConnected()
OnPlayerDisconnected()
OnPostRender()
OnPreCull()
OnPreRender()
OnRenderImage()
OnRenderObject()
OnSerializeNetworkView()
OnServerInitialized()
OnTransformChildrenChanged()
OnTransformParentChanged()
OnTriggerEnter()
OnTriggerEnter2D()
OnTriggerExit()
OnTriggerExit2D()
OnTriggerStay()
OnTriggerStay2D()
OnValidate()
OnWillRenderObject()
Reset()
Start()
Update()
Some useful code snippets:
Debug.Log()
(typelog
)Debug.LogError()
(typelogerror
)Debug.LogWarning()
(typelogwarning
)Debug.LogException()
(typelogexception
)
If you have any suggestions, open an issue in the Github project page and I'll add it as soon as I can :).
If you like the color theme of the previews, you can download it here: Base16 Ocean Dark Extended Theme.
Thanks you for downloading this extension.
They both look similar and behave similarly, what's then the difference between OnTriggerEnter
and OnCollisionEnter
? The key to understand this is in knowing what are triggers in Unity.
In Unity, an object might be controlled by the physics engine or by script. If you need to control an object by script but still would like to know if an object touched another, a 'collision' happened, you need to use triggers.
Collision is under quotes because, strictly under Unity's terminology, a collision only happens when object's movements are governed by the physics engine, for the other cases what we have are simply objects touching each other, also, for such event, our script can be alerted as well.
What are triggers
A trigger is a collider that's not influenced by the physics engine. It doesn't respond to forces nor gravity. But they still do have a use for the physics engine, they are used to detect whether an object passed through another. Triggers are everywhere in Unity game development, and in other engines too to be honest.
This Reaper is controlled by a simple back-and-forth walk AI, the physics engine is not used, but we still want to know when it has touched some things in the stage. For that, we can use an OnTriggerEnter
Collisions
A collision is also the result of an object touching another one, but instead of passing through, these objects push each other in a realistic way. Use OnCollisionEnter
when your rigidbody colliders aren't triggers and you'd like to know when they touched each other.
For more information on how to create and use triggers, please see the official documentation on the subject. If you followed the documentation and something with your collision detection is not working, you may try to fix it using our comprehensive collision fixing tutorial.
TLDR;
Use triggers if you don't want/need the physics engine to control your object but still need to know if an object passed through another or reached some `zone` within the game. In that case, you'll use OnTriggerEnter()
.
Ontriggerenter2d Unity
If your object is indeed controlled by the physics engine, you'll use OnCollisionEnter()
to know if an object touched another one.