Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  How to attach a GameObject to a rope from script?
#2
Quote:My idea is in the collision event of the rope and objects, I create a ControlPoint in the path of the rope,

You can't create a control point at runtime. Or rather, it won't have any effect. Blueprints behave just like prefabs: once you instantiate a prefab, the instance will no longer be affected by changes made to the prefab. It's a "snapshot" of the prefab at the moment of instantiating it. Similarly, once you create an actor out of a blueprint, modifying the blueprint has no effect on the actor/actors instantiated from it.

Control points only exist in blueprints, to aid you in setting the initial shape and properties of the rope. At runtime, actors only contain particles and constraints.

Quote:1. particlePos is the position of the rope, not the collided point

You're retrieving the position of the rope actor:

Code:
solver.particleToActor[contact.bodyA].actor.transform.position;

You want the position of the particle that has collided, so use this instead:

Code:
solver.positions[solver.simplices[contact.bodyA]];

See:
http://obi.virtualmethodstudio.com/tutor...sions.html


Quote:2. Got error "The particle group 'Control point' references a particle that does not exist in the actor 'Obi Rope'."

Because that particle does not exist. You've regenerated the blueprint, but as I explained that does not change any existing ropes in the scene. In addition to thisĀ  you're accessing the groups array using a particle index, which does not make any sense (can result in out of bounds access exceptions):

Code:
attachment.particleGroup = blueprint.groups[particleIndex];

Should be:

Code:
attachment.particleGroup = blueprint.groups[groupIndex];

If your blueprint has 3 control points, "groupIndex" can only be 0, 1, or 2.


Quote:There is a rope and some objects in a table. I drag the rope and move, when the rope collide with an object, the object should be attached to the rope.
How should I implement this?

If you want two-way coupling between the rope and the object, you can create a new pin constraint at runtime. That's the simplest way. See adding/removing constraints in the "scripting constraints" section of the manual:

http://obi.virtualmethodstudio.com/tutor...aints.html

Internally, this is what a dynamic attachment does: it creates a pin constraint between the particle index corresponding to a control point, and a target object.
Reply


Messages In This Thread
RE: How to attach a GameObject to a rope from script? - by josemendez - 09-06-2021, 08:24 AM