Implement auto-sizing for box colliders when they are added to the scene.

This commit is contained in:
Menotdan
2023-12-09 17:43:06 -05:00
parent 306dd43b18
commit 4e54e945ef
3 changed files with 38 additions and 0 deletions

View File

@@ -534,6 +534,8 @@ namespace FlaxEditor.Modules
{
node.ParentNode = parentNode;
}
actor.OnActorSpawned();
}
private void OnActorDeleted(Actor actor)

View File

@@ -116,6 +116,15 @@ namespace FlaxEngine
LocalTransform = Transform.Identity;
}
/// <summary>
/// Called in-editor when an actor is added to the scene.
/// If not in the editor, this function will not be called.
/// </summary>
public virtual void OnActorSpawned()
{
}
/// <summary>
/// Creates a new child actor of the given type.
/// </summary>

View File

@@ -0,0 +1,27 @@
using System;
namespace FlaxEngine
{
partial class BoxCollider
{
/// <inheritdoc />
public override void OnActorSpawned()
{
base.OnActorSpawned();
if (Parent is StaticModel model)
{
Vector3 modelScale = model.Scale;
Vector3 modelSize = model.Box.Size;
Vector3 modelCenter = model.Box.Center - model.Position;
Vector3 colliderSize = modelSize / modelScale;
Vector3 colliderCenter = modelCenter / modelScale;
Size = colliderSize;
Center = colliderCenter;
// Undo Rotation
Orientation *= Quaternion.Invert(Orientation);
}
}
}
}