- Home /
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?
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 );
}
}
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.
Make sure you're using Physics2D.Simulate
and OnTriggerEnter2D
then
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
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
Fist Passing Through The Target 2 Answers
How is drag applied to force? 3 Answers
RigidbodyConstraints.None issue 0 Answers
[Physics]Forklift Problem. 1 Answer