Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Change obi rope attachment target realtime with script
#4
(09-10-2023, 05:27 PM)Adammm Wrote: My ropes are already connected to the player hands, like I said before whenever I change the target with script,nothing happens.

As I said what will happen is that the rope will be re-connected to the new target but won't cause the rope to snap to a new position, it will keep its position relative to the previous target. This is explained in the manual:

Quote:It's important to realize that attachments do not move the particle group to the position of the target object, nor the target object to the position of the particle group. Moving the particle group to the target's position would collapse all particles into a single point, and would result in a kickback effect once the particles spring back to their original positions. Moving the target could cause a whole palette of issues depending on your game logic.

(09-10-2023, 05:27 PM)Adammm Wrote: But if I do it manually (like in the video) then it will snap to the new position. What I need is exactly like in the video (snapping to new position) but with script.

Quoting the manual: "If you want particles attached at a specific position other than their position at the time of creating the attachment, you can set their position at runtime before creating the attachment: see scripting particles."

In other words, you just need to set the position of the attached particles before changing the attachment target via script. This is easy to do: the following code will move the particle to the same local space position in the new target object, just like it happens in the editor:

Code:
// get the index of the attached particle in the rope and the solver:
int actorIndex = attachment.particleGroup.particleIndices[0];
int solverIndex = rope.solverIndices[actorIndex];

// get particle position relative to the current target:
var worldPos = rope.solver.transform.TransformPoint(rope.solver.positions[solverIndex]);
var localPos = attachment.target.InverseTransformPoint(worldPos);

// disable attachment:
attachment.enabled = false;

// move particle to new same local position, relative to the new target:
rope.solver.positions[solverIndex] = rope.solver.transform.InverseTransformPoint(newTarget.transform.TransformPoint(localPos));

// set new target and re-enable the attachment:
attachment.target = newTarget;
attachment.enabled = true;

The only tricky bit is to convert from the target's local space to the solver's local space and vice-versa, using Unity's TransformPoint /InverseTransformPoint.

kind regards,
Reply


Messages In This Thread
RE: Change obi rope attachment target realtime with script - by josemendez - 10-10-2023, 06:34 AM