• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by OberynMartell · Dec 28, 2021 at 11:05 PM · physics

Collision Events on Edit Mode Tests

Hi!

I'm executing Edit Mode tests that include physics by simulating the steps manually. The rigid bodies are moving correctly but they don't seem to generate collision events when they collide with each other.

I already tried using the ExecuteInEditMode attribute and the runInEditMode property but they seem to only cause the Awake, Start and Update methods to be called, but no OnTriggerEnter.

Is it possible to generate them on Edit Mode?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Dec 28, 2021 at 11:07 PM

idk, works fine

MyTestComponent.cs

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class MyTestComponent : MonoBehaviour
 {
     void OnTriggerEnter ( Collider other ) => Debug.Log($"{this.name}<{nameof(MyTestComponent)}>.OnTriggerEnter( {other.name} )");
     void OnCollisionEnter ( Collision collision ) => Debug.Log($"{this.name}<{nameof(MyTestComponent)}>.OnCollisionEnter( {collision.collider.name} )");
 }

EditorPhysicsController.cs

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class EditorPhysicsController : MonoBehaviour
 {
     [Header("disable autoSimulation to trigger the simulation in edit mode")]
     [SerializeField] bool _autoSimulation = true;
     [SerializeField][Min(9.809089e-45f)] float _physicsStep = 1f / 30f;
     void OnValidate () => Physics.autoSimulation = _autoSimulation;
     void Update ()
     {
         if( !Physics.autoSimulation && !Application.isPlaying )
             Physics.Simulate( _physicsStep );
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OberynMartell · Dec 28, 2021 at 11:45 PM 0
Share

Yeap, that seems to work correctly. However, I tried the same thing with the 2D Physics and it doesn't seem to be working (the objects collide but there is no event). Sorry, I didn't say in the post that it was with 2D Physics because I assumed it would behave the same.

avatar image andrew-lukasik OberynMartell · Dec 28, 2021 at 11:50 PM 0
Share

Make sure you're using Physics2D.Simulate and OnTriggerEnter2D then

avatar image OberynMartell andrew-lukasik · Dec 28, 2021 at 11:56 PM 0
Share

Yeah, I made sure of that. I even tried having autoSimulation = false, the Update method with just Physics2D.Simulate( _physicsStep ) and then when I press play everything works correctly.

Edit: This is the code converted to 2D:

 using UnityEngine;
 
 [ExecuteInEditMode]
  public class EditorPhysicsController2D : MonoBehaviour
  {
      [Header("disable autoSimulation to trigger the simulation in edit mode")]
      [SerializeField] bool _autoSimulation = true;
      [SerializeField][Min(9.809089e-45f)] float _physicsStep = 1f / 30f;
      void OnValidate () => Physics2D.autoSimulation = _autoSimulation;
      void Update ()
      {
          if( !Physics2D.autoSimulation && !Application.isPlaying )
              Physics2D.Simulate( _physicsStep );
      }
  }
 


 using UnityEngine;
  
  [ExecuteInEditMode]
  public class MyTestComponent2D : MonoBehaviour
  {
      void OnTriggerEnter2D ( Collider2D other ) => Debug.Log($"{this.name}<{nameof(MyTestComponent)}>.OnTriggerEnter( {other.name} )");
      void OnCollisionEnter2D ( Collision2D collision ) => Debug.Log($"{this.name}<{nameof(MyTestComponent)}>.OnCollisionEnter( {collision.collider.name} )");
  }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D 360 degress platformer example needed 0 Answers

Character Controller + Animation Not Working As It Should 0 Answers

Sometimes the ball isn't moving 0 Answers

How to climb Ledges? 0 Answers

Humanoid Locomotion in Unity,Trying to implement locomotion in Unity (with ml-agents) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges