18-09-2023, 02:44 PM
(This post was last modified: 18-09-2023, 02:45 PM by aderae.
Edit Reason: fix code
)
Code:
ObiRopeBlueprint blueprint = ScriptableObject.CreateInstance<ObiRopeBlueprint>();
int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 0);
blueprint.pooledParticles = 0;
blueprint.thickness = Config.Thickness;
blueprint.resolution = 2;
//particleCount = ropeLength / ropeThickness * resolution
blueprint.path.Clear();
blueprint.path.AddControlPoint(Config.Front.localPosition, -Vector3.right, Vector3.right, Vector3.up, 0.1f, 0.1f, 0.8f, filter, Color.white, "start");
blueprint.path.AddControlPoint(Config.Back.localPosition, -Vector3.right, Vector3.right, Vector3.up, 0.1f, 0.1f, 0.8f, filter, Color.white, "end");
blueprint.path.RecalculateLenght(Matrix4x4.identity, 0.00001f, 7);
float totalParticleCount = 30;
blueprint.resolution = totalParticleCount * Config.Thickness / blueprint.path.Length;
blueprint.path.FlushEvents();
ObiParticleAttachment frontAttachment = ObiRope.gameObject.AddComponent<ObiParticleAttachment>();
frontAttachment.attachmentType = ObiParticleAttachment.AttachmentType.Static;
frontAttachment.target = Config.Front;
frontAttachment.particleGroup = blueprint.groups[0];
ObiParticleAttachment backAttachment = ObiRope.gameObject.AddComponent<ObiParticleAttachment>();
backAttachment.attachmentType = ObiParticleAttachment.AttachmentType.Static;
backAttachment.target = Config.Back;
backAttachment.particleGroup = blueprint.groups[1];
ObiRope.OnBlueprintLoaded += ObiRopeLoaded;
ObiRope.ropeBlueprint = blueprint;
ObiFixedUpdater.solvers.Add(ObiSolver);
Code:
private void ObiRopeLoaded(ObiActor actor, ObiActorBlueprint blueprint)
{
// load particle positions
for (int i = 0; i < ObiSolver.positions.count; i++)
{
if (i < ParticlePositions.Count)
{
ObiSolver.positions.SetVector3(i, ParticlePositions[i]);
}
ObiSolver.velocities.SetVector3(i, Vector3.zero);
}
ParticlePositions.Clear();
}
As I said, if I wait 2 seconds and run the code in ObiRopeLoaded part, all works perfectly fine