Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Change obi rope attachment target realtime with script
#1
Hey! I was trying to change obi rope attachment the other day with script, and I couldn't make it happen. Whenever I do it manually in the inspector while playing the rope snaps to the new target position. But if I do it with script (just change the attachment script target to new transform), it changes the target but won't change the position nor do anything. 

Can somebody help me with this and if fix the issue?
Reply
#2
(06-10-2023, 03:26 PM)Adammm Wrote: Hey! I was trying to change obi rope attachment the other day with script, and I couldn't make it happen. Whenever I do it manually in the inspector while playing the rope snaps to the new target position.

Hi,

Attachments should not snap to a new position when changing their target, there's nothing in their code that enforces this. By default they just attach particles at their current position relative to the target:
http://obi.virtualmethodstudio.com/manua...ments.html

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.

Instead, particles in the group are attached at their current position relative to the target object. This makes it simple to work with multiple particles, and ensures smooth attachments with no initial "jumps" or sudden motions.

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.

(06-10-2023, 03:26 PM)Adammm Wrote: But if I do it with script (just change the attachment script target to new transform), it changes the target but won't change the position nor do anything. 

If you want to change the position of the particles before attaching them, you need to do that manually. See:
http://obi.virtualmethodstudio.com/manua...ments.html

kind regards.
Reply
#3
My ropes are already connected to the player hands, like I said before whenever I change the target with script,nothing happens. 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.


https://streamable.com/ao36e0
Reply
#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