Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RESET Obi Softbody
#11
So just a quick update on my journey of resetting softbodies Guiño

Weirdly, the two-line script using the teleport still crashed my editor. I created a completely empty scene only with a solver and a softbody. When I reset its position and exit playmode afterwards, Unity crashes. BUT, your other script does work - I only had to swap the "count" parts to "Length".

Here's the adjusted script that lets Unity live (in my case):


using UnityEngine;
using Obi;

[RequireComponent(typeof(ObiSoftbody))]
public class ResetActor : MonoBehaviour
{
    ObiSoftbody softbody;
    Vector4[] originalPositions;
    Quaternion[] originalOrientations;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.G))
        {
            ResetToOriginalPosition();
        }
    }

    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.Length];
        originalOrientations = new Quaternion[softbody.solverIndices.Length];

        for (int i = 0; i < softbody.solverIndices.Length; ++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.Length; ++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;
        }
    }
}


I know that the teleport shouldn't cause any problems, but Unity is a fickle bitch and maybe it helps any other person who has the same bug as me.

Cheers
Felix
Reply


Messages In This Thread
RESET Obi Softbody - by swancollective - 22-06-2023, 08:25 PM
RE: RESET Obi Softbody - by swancollective - 23-06-2023, 11:17 AM
RE: RESET Obi Softbody - by josemendez - 23-06-2023, 02:19 PM
RE: RESET Obi Softbody - by swancollective - 23-06-2023, 06:00 PM
RE: RESET Obi Softbody - by josemendez - 23-06-2023, 06:42 PM
RE: RESET Obi Softbody - by swancollective - 24-06-2023, 11:52 AM
RE: RESET Obi Softbody - by josemendez - 24-06-2023, 02:48 PM
RE: RESET Obi Softbody - by swancollective - 25-06-2023, 10:15 AM
RE: RESET Obi Softbody - by josemendez - 25-06-2023, 11:06 AM
RE: RESET Obi Softbody - by swancollective - 25-06-2023, 12:39 PM
RE: RESET Obi Softbody - by swancollective - 03-07-2023, 07:20 AM
RE: RESET Obi Softbody - by spikebor - 09-05-2024, 06:12 AM