Attach custom data to Physics Core 2D API objects
Attach values and physics masks to physics objects so you can fetch them later.
Read time 1 minuteLast updated 13 days ago
To attach custom data to an object you create using the Physics Core 2D API, use the type.
PhysicsUserDataYou can attach data to the following types:
WorldBodyShapeChainJoint
Follow these steps:
-
Create ainstance and populate it with your custom fields. For example:
PhysicsUserDataPhysicsUserData physicsUserData = new PhysicsUserData { customObject = this, customBool = true, customFloat = 123.4f, customInt = 567, customPhysicsMask = PhysicsMask.All }; -
Assign ainstance to the
PhysicsUserDataproperty of the physics object. For example, to assign the custom data to a shape:userDatamyShape.userData = physicsUserData; -
To fetch the data, for example when you detect a collision, get theproperty of the physics object. For example:
userDataDebug.Log(myShape.userData.intValue);