Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Moving a Particle Attachment at runtime problems
#1
Exclamación 
I'm trying to attach an object at an arbitrary point on a rope (wherever the closest particle is to where it collides with the rope).

Requires a bit of context - For various reason I also want to avoid putting Obi Rigidbodies on everything the rope might interact with, so I have a single invisible Obi Rigidbody that always just stays attached to the rope at some point, and my other game objects attach (via Unity Joints) to that Obi Rigidbody whenever they are going to attach to the rope. That Obi Rigidbody is supposed to move itself around on the rope, at the time of the other object attaching, to the nearest particle.

The workflow is:

- Obi Particle Attachment is pre-configured on the rope, attached to an invisible small Obi Rigidbody (ObiR) in the middle of the rope.
- Some object (ObjA) enters the rope's trigger zone
- I iterate through the rope's particles to get the nearest particle to ObjA.
- I set Obi Particle Attachment's "attached Transform" to null, disconnecting ObiR
- I modify the Particle Attachment's Particle Group List (which only contains a single particle) to contain only that nearest particle I found.
- I manually teleport ObiR to that same particle's position
- I call ObiR.UpdateIfNeeded()
- I also call Physics2D.SyncTransforms() just in case
- I set Obi Particle Attachment's "attached Transform" back to ObiR, re-attaching it at its new position - but it just snaps back to its old position.
- (Then I attach ObjA to ObiR with a Unity joint)

I can see the Particle Group be modified correctly in realtime.

However, where this falls down is that ObiR, which is attached to that Particle Group, does *not* follow that Particle Group position, regardless of whether or not I manually move it there first before letting the simulation continue. ObiR snaps back to its original position and stays that distance away from its new particle, as though its anchor point to the rope has compensated for the changed particle index.

What am I doing wrong, and what I can change to have the desired effect?

Well, now I feel dumb. The problem was here:

- I manually teleport ObiR to that same particle's position

I was using rigidbody.position to do this.
But I guess this waits for the next physics update or something.
Just setting transform.position fixed it.

According to Unity you are not supposed to move rigidbodies this way, but I can't think of what else to do if I want it to move immediately.
Reply
#2
(17-09-2020, 12:40 AM)dasbin Wrote: I was using rigidbody.position to do this.
But I guess this waits for the next physics update or something.
Just setting transform.position fixed it.
According to Unity you are not supposed to move rigidbodies this way, but I can't think of what else to do if I want it to move immediately.

rigidbody.position does wait for the next physics update, according to the documentation:
https://docs.unity3d.com/ScriptReference...ition.html

Quote:If you change the position of a Rigibody using Rigidbody.position, the transform will be updated after the next physics simulation step.

So rigidbody.position and transform.position are not functionally equivalent.

There's nothing wrong with setting the position using transform.position, as long as that's what you're after. I guess the warning is aimed at people that expect to be able to physically interact with rigidbodies by setting their transform instead of applying forces/impulses/accelerations.
Reply