23-06-2023, 06:42 PM
(This post was last modified: 23-06-2023, 06:47 PM by josemendez.)
(23-06-2023, 06:00 PM)swancollective Wrote: Thank you so much for the quick reply and your code - IT WORKS!!
Only thing, since adding the script, Unity editor keeps crashing on me. Maybe weird coincidence, but it's the only thing I changed, and it never crashed before. Maybe any clue why this could be?
The code above simply iterates trough a couple arrays setting values internally, there's no reason I can think for it to crash. Could you share your editor log, so that I may see the crash stack trace?
(23-06-2023, 06:00 PM)swancollective Wrote: As to your question, this is GPT-4s best attempt of resetting the softbody (which also didn't work, after hours of iterating):
The most likely outcome of that script is a NullRefException at runtime. It doesn't check whether the blueprint is loaded into a solver and simply assumes it to be by the time Awake() is called - which is often not the case, hence causing a null ref when it tries to access the length of the solverIndices. It also omits particle velocities and orientations which should also be reset for the softbody to correctly go back to its initial state, and assumes the solver is at the scene origin.
Here's the closest correct version of it (still assuming the solver is at the origin) even then it's far more verbose and complex than it needs to be - ResetParticles() and Teleport() do a better job in just two lines.
Code:
using UnityEngine;
using Obi;
[RequireComponent(typeof(ObiSoftbody))]
public class ResetSoftbody : MonoBehaviour
{
ObiSoftbody softbody;
Vector4[] originalPositions;
Quaternion[] originalOrientations;
void OnEnable()
{
softbody = GetComponent<ObiSoftbody>();
softbody.OnBlueprintLoaded += Softbody_OnBlueprintLoaded;
if (softbody.isLoaded)
Softbody_OnBlueprintLoaded(softbody, softbody.softbodyBlueprint);
}
void OnDisable()
{
softbody.OnBlueprintLoaded -= Softbody_OnBlueprintLoaded;
}
private void Softbody_OnBlueprintLoaded(ObiActor actor, ObiActorBlueprint blueprint)
{
originalPositions = new Vector4[softbody.solverIndices.count];
originalOrientations = new Quaternion[softbody.solverIndices.count];
for (int i = 0; i < softbody.solverIndices.count; ++i)
{
int solverIndex = softbody.solverIndices[i];
originalPositions[i] = softbody.solver.positions[solverIndex];
originalOrientations[i] = softbody.solver.orientations[solverIndex];
}
}
public void ResetToOriginalPosition()
{
for (int i = 0; i < softbody.solverIndices.count; ++i)
{
int solverIndex = softbody.solverIndices[i];
softbody.solver.positions[solverIndex] = originalPositions[i];
softbody.solver.orientations[solverIndex] = originalOrientations[i];
softbody.solver.velocities[solverIndex] = softbody.solver.angularVelocities[solverIndex] = Vector4.zero;
}
}
}