I want to put this in my behaviour class, so when an object collides with another it will invoke an event.
documentation for the modding.
This is the code for the object
ModAPI.Register(
new Modification()
{
OriginalItem = ModAPI.FindSpawnable("Stick"),
NameOverride = "Ram Stick",
DescriptionOverride = "Looks like it holds 128 megabytes of memory, wouldn't be useful today.",
CategoryOverride = ModAPI.FindCategory("Machinery"),
ThumbnailOverride = ModAPI.LoadSprite("sprites/ramstick_thumb.png"),
AfterSpawn = (Instance) =>
{
//Sets ramstick sprite when spawned and scales it down
Instance.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
Instance.GetComponent<SpriteRenderer>().sprite = ModAPI.LoadSprite("sprites/ramstick.png");
Instance.AddComponent<RamstickBehavior>();
Instance.FixColliders();
}
}
and this is my behaviour.
public class RamstickBehavior : MonoBehaviour
{
}
I am kind of new to C# if anyone could help me set this up it would be appreciated, or give me examples or some pages where i can do research on this, but im not exactly sure where to look.
CodePudding user response:
Add a Collider2D and use OnCollisionEnter, which is automatically called when the collision is detected.
public class RamstickBehavior : MonoBehaviour
{
Collider2D myCollider; // or just add in the editor and/or make this public and drag a collider from the editor
void Start()
{
myCollider = GetComponent<Collider2D>(); // alternatively, from the code like this
}
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
// invoke an event
}
}
}