Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Assigning target to ParticleAttchment component at runtime
#1
Pregunta 
Assigning target through editor works like intended but assigning it during play works somewhat. You can tell the attached object is attached to the rope but  at different position and the rope end at which the particle group its attached to doesnt seem to be really attached.

I can create a gif of what im talking about but a lil busy atm, when im free ill add it in but pretty easy to recreate.
Reply
#2
(20-12-2019, 07:33 AM)VirtualCucumber Wrote: Assigning target through editor works like intended but assigning it during play works somewhat. You can tell the attached object is attached to the rope but  at different position and the rope end at which the particle group its attached to doesnt seem to be really attached.

I can create a gif of what im talking about but a lil busy atm, when im free ill add it in but pretty easy to recreate.

Works fine for me. Keep in mind that setting the target or particle group at runtime, will attach the particles at their current positions. So if the rope is being simulated, the position will of course be different of the one it would be used at editor time.

If you wish to make sure they're at a certain position, just move the particles in the particle group prior to attaching them.

If you can provide a picture/gif of your issue I will be glad to help further. Sonrisa
Reply
#3
(20-12-2019, 08:41 AM)josemendez Wrote: Works fine for me. Keep in mind that setting the target or particle group at runtime, will attach the particles at their current positions. So if the rope is being simulated, the position will of course be different of the one it would be used at editor time.

If you wish to make sure they're at a certain position, just move the particles in the particle group prior to attaching them.

If you can provide a picture/gif of your issue I will be glad to help further. Sonrisa

Here's a quick test https://imgur.com/a/k0qjzqK

The script is simple:
Code:
using UnityEngine;
using Obi;

public class DummyAttach : MonoBehaviour
{
   public Transform target;
   private ObiParticleAttachment _attachment;

   private void Start()
   {
       _attachment = GetComponent<ObiParticleAttachment>();

       if (target != null)
           _attachment.target = target;
   }
}
Reply
#4
(20-12-2019, 09:56 AM)VirtualCucumber Wrote: Here's a quick test https://imgur.com/a/k0qjzqK

The script is simple:
Code:
using UnityEngine;
using Obi;

public class DummyAttach : MonoBehaviour
{
   public Transform target;
   private ObiParticleAttachment _attachment;

   private void Start()
   {
       _attachment = GetComponent<ObiParticleAttachment>();

       if (target != null)
           _attachment.target = target;
   }
}

Quite probably, your rope is colliding with the yellow blocks during the first frame. So it is projected outside, gaining speed, and after the first frame Start() is executed and the rope bound in its current position.

Either:
- Deactivate collisions between the yellow blocks and the first few particles in the rope (recommended), setting either to the same phase as the other (See the last bit of: http://obi.virtualmethodstudio.com/tutor...aints.html)
- Force the rope particle group position in Start(), right before setting the target.
Reply
#5
(20-12-2019, 10:02 AM)josemendez Wrote: Quite probably, your rope is colliding with the yellow blocks during the first frame. So it is projected outside, gaining speed, and after the first frame Start() is executed and the rope bound in its current position.

Either:
- Deactivate collisions between the yellow blocks and the first few particles in the rope (recommended), setting either to the same phase as the other (See the last bit ofTristehttp://obi.virtualmethodstudio.com/tutor...aints.html)
- Force the rope particle group position in Start(), right before setting the target.

Through Awake its fine.

I set the particleGroups made on that BP to 0 collision phase and then by default the ObiCollider is 0 for the block and still same thing.

I then I added this fast into the script to manually set the position tooo
Code:
using UnityEngine;
using Obi;

public class DummyAttach : MonoBehaviour
{
   public Transform target;

   private ObiRope _rope;
   private ObiParticleAttachment _attachment;

   private void Start()
   {
       _rope = GetComponent<ObiRope>();
       _attachment = GetComponent<ObiParticleAttachment>();

       var index = _rope.solverIndices[0];
       var targetPos = _rope.solver.transform.InverseTransformPoint(target.position);
       _rope.solver.invMasses[index] = 0f;
       _rope.solver.positions[index] = targetPos;

       if (target != null)
           _attachment.target = target;

       //_rope.solver.invMasses[index] = 1f;
   }
}

Resetting the mass is commented out because it just did what its been doing which is attaching to that offset we see in the gif. Not resetting it just attaches it manually with no 2-way physics between the rope and rigidbody which is good for just positioning but I really enjoy the new attachment system and would like to attach through runtime.
Reply
#6
(20-12-2019, 10:19 AM)VirtualCucumber Wrote: Through Awake its fine.

I set the particleGroups made on that BP to 0 collision phase and then by default the ObiCollider is 0 for the block and still same thing.

I then I added this fast into the script to manually set the position tooo
Code:
using UnityEngine;
using Obi;

public class DummyAttach : MonoBehaviour
{
   public Transform target;

   private ObiRope _rope;
   private ObiParticleAttachment _attachment;

   private void Start()
   {
       _rope = GetComponent<ObiRope>();
       _attachment = GetComponent<ObiParticleAttachment>();

       var index = _rope.solverIndices[0];
       var targetPos = _rope.solver.transform.InverseTransformPoint(target.position);
       _rope.solver.invMasses[index] = 0f;
       _rope.solver.positions[index] = targetPos;

       if (target != null)
           _attachment.target = target;

       //_rope.solver.invMasses[index] = 1f;
   }
}

Resetting the mass is commented out because it just did what its been doing which is attaching to that offset we see in the gif. Not resetting it just attaches it manually with no 2-way physics between the rope and rigidbody which is good for just positioning but I really enjoy the new attachment system and would like to attach through runtime.

Doing the same thing in Awake should also work because you attach the rope immediately, it has no chance to react to collisions.

Regarding the phase (which is set in the path editor, and setting the control point phase value), make sure the rope simply falls down if no attachment is setup. Should not react to the collider at all if done correctly.
Reply
#7
(20-12-2019, 10:40 AM)josemendez Wrote: Doing the same thing in Awake should also work because you attach the rope immediately, it has no chance to react to collisions.

Regarding the phase (which is set in the path editor, and setting the control point phase value), make sure the rope simply falls down if no attachment is setup. Should not react to the collider at all if done correctly.

It just falls. I set all particle groups to 0 so it wont collide with any ObiCollider in scene with default phase of 0
Reply
#8
Just hopped back on this project and still wondering how to attach at runtime.

[Image: dt7D3Hz.gif]

The rope just has ObiAttachment and AttachRuntime component on it, the solvers gravity is set to 0 and the collision phases on particles and object are the same for no collisions.

AttachRuntime.cs:

Code:
using UnityEngine;
using Obi;

public class AttachRuntime: MonoBehaviour
{
   public ObiRope rope;
   public ObiParticleAttachment attachment;
   public Transform attachmentObject;

   private PlayerActions _input;

   private void Start()
   {
       _input = PlayerActions.CreateWithDefaultBindings();
   }

   private void Update()
   {
       if (_input.Interact.WasPressed)
       {
           attachment.target = attachmentObject;
       }
   }
}


The result is in the gif. I did try to force the position to the attachmentObject position but as soon as you reset the mass back to default it snaps back to the position in the gif:

Code:
attachment.target = attachmentObject;

var index = rope.solverIndices[0];
var targetPos = rope.solver.transform.InverseTransformPoint(attachmentObject.position);
rope.solver.invMasses[index] = 0f;
rope.solver.positions[index] = targetPos;
rope.solver.invMasses[index] = 0.1f;
Reply