08-09-2021, 10:18 PM
Good day,
I am having troubles spawning ropes from the code.
I wrote my code very similar to GrapplingHook.cs (a sample scene).
I now have the problem that the so-spawned ropes have no collision. Here is the code from that.
Any ideas where the problem could be?
I am having troubles spawning ropes from the code.
I wrote my code very similar to GrapplingHook.cs (a sample scene).
I now have the problem that the so-spawned ropes have no collision. Here is the code from that.
Any ideas where the problem could be?
Code:
public void SpawnWire(Vector3 end1, Vector3 localHit)
{
// Create the rope
GameObject emptyGameObjectForOneRope = new GameObject();
emptyGameObjectForOneRope.transform.parent = this.transform;
rope = emptyGameObjectForOneRope.AddComponent<ObiRope>();
ropeRenderer = emptyGameObjectForOneRope.AddComponent<ObiRopeExtrudedRenderer>();
//ropeRenderer.section = section;
ropeRenderer.uvScale = new Vector2(1, 5);
ropeRenderer.normalizeV = true;
ropeRenderer.uvAnchor = 0;
ropeRenderer.thicknessScale = 0.5f;
rope.GetComponent<MeshRenderer>().material = material;
// Setup a blueprint for the rope:
blueprint = ScriptableObject.CreateInstance<ObiRopeBlueprint>();
blueprint.resolution = 0.5f;
blueprint.thickness = 0.5f;
blueprint.pooledParticles = 100;
// Tweak rope parameters:
rope.selfCollisions = true;
rope.surfaceCollisions = true;
rope.collisionMaterial = obiCollisionMaterial;
// Add a cursor to be able to change rope length:
cursor = emptyGameObjectForOneRope.AddComponent<ObiRopeCursor>();
cursor.cursorMu = 0;
cursor.direction = true;
////////////////////////////////////////////////////
this.end1 = end1;
this.localHit = localHit;
StartCoroutine("CoRoutineXD");
// Procedurally generate the rope path (a simple straight line):
}
public IEnumerator CoRoutineXD()
{
yield return 0;
// Procedurally generate the rope path (not quite a simple straight line, I could change it back though):
blueprint.path.Clear();
blueprint.path.AddControlPoint(end1, Vector3.back, Vector3.forward, Vector3.up, 0.1f, 0.1f, 1, 1, Color.white, "Hook start");
blueprint.path.AddControlPoint(localHit, Vector3.forward, Vector3.back, Vector3.up, 0.1f, 0.1f, 1, 1, Color.white, "Hook end");
blueprint.path.FlushEvents();
// Generate the particle representation of the rope (wait until it has finished):
yield return blueprint.Generate();
// Set the blueprint (this adds particles/constraints to the solver and starts simulating them).
rope.ropeBlueprint = blueprint;
rope.GetComponent<MeshRenderer>().enabled = true;
//
// Pin both ends of the rope (this enables two-way interaction between character and rope):
var pinConstraints = rope.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints<ObiPinConstraintsBatch>;
pinConstraints.Clear();
var batch = new ObiPinConstraintsBatch();
batch.AddConstraint(rope.solverIndices[0], gameObject.GetComponent<ObiCollider>(), localHit, Quaternion.identity, 0, 0, float.PositiveInfinity);
batch.AddConstraint(rope.solverIndices[blueprint.activeParticleCount - 1], gameObject.GetComponent<ObiCollider>(),
end1, Quaternion.identity, 0, 0, float.PositiveInfinity);
batch.activeConstraintCount = 2;
pinConstraints.AddBatch(batch);
rope.SetConstraintsDirty(Oni.ConstraintType.Pin);
cursor.ChangeLength(wireLength);