Hi all,
I don't know if I should create a new thread or not, but I figured out how to do it and that could interest someone else.
So, just to remind, I was trying to "force" the rope to have the firsts particles aligned when I grab one or both of the Start and EndPrefab. ( see my previous post )
Just an update, since last time I have added the possibility to grab the ObiRope following this thread and the VRTK_ObiRopeInteraction and VRTK_ObiRopeSolver scripts.
What I did is putting my RJ45 Prefab, which is grabbable, as a StartPrefab and EndPrefab of the ObiRope and modified the PlaceObjectAtCurveFrame's function in the ObiRope script.
Actually, this function is replacing, every frame, the Start and End GameObject following the curve of the rope (position + rotation). So when I'm grabbing the rope, the prefabs are moving properly according to it. But the thing is I don't want that the rope "control" the GameObject's orientation while I grabbing it but conversely, I want to control the curve of the rope (not to twist it) while I grabbing a cable end.
This is my code :
I hope it's going to be helpful if someone wants to do the same . Please ask if you have any question about the code or how I did it.
I don't know if I should create a new thread or not, but I figured out how to do it and that could interest someone else.
So, just to remind, I was trying to "force" the rope to have the firsts particles aligned when I grab one or both of the Start and EndPrefab. ( see my previous post )
Just an update, since last time I have added the possibility to grab the ObiRope following this thread and the VRTK_ObiRopeInteraction and VRTK_ObiRopeSolver scripts.
What I did is putting my RJ45 Prefab, which is grabbable, as a StartPrefab and EndPrefab of the ObiRope and modified the PlaceObjectAtCurveFrame's function in the ObiRope script.
Actually, this function is replacing, every frame, the Start and End GameObject following the curve of the rope (position + rotation). So when I'm grabbing the rope, the prefabs are moving properly according to it. But the thing is I don't want that the rope "control" the GameObject's orientation while I grabbing it but conversely, I want to control the curve of the rope (not to twist it) while I grabbing a cable end.
This is my code :
Code:
private void PlaceObjectAtCurveFrame(CurveFrame frame, GameObject obj, Space space, bool reverseLookDirection)
{
// Check if the GameObject is grabbed or not
if (obj.GetComponent<GrabbableBehaviour>().IsGrabbed == false && obj.GetComponent<PlugMaleBehaviour>().IsPlugged == false)
{
// If not, I'm using the default function
if (space == Space.Self)
{
Matrix4x4 l2w = transform.localToWorldMatrix;
obj.transform.position = l2w.MultiplyPoint3x4(frame.position);
if (frame.tangent != Vector3.zero)
obj.transform.rotation = Quaternion.LookRotation(l2w.MultiplyVector(reverseLookDirection ? frame.tangent : -frame.tangent),
l2w.MultiplyVector(frame.normal));
}
else
{
obj.transform.position = frame.position;
if (frame.tangent != Vector3.zero)
obj.transform.rotation = Quaternion.LookRotation(reverseLookDirection ? frame.tangent : -frame.tangent, frame.normal);
}
// Resetting of the mass of the two last or first particles
if (reverseLookDirection)
{
invMasses[invMasses.Length - 1] = 0.1f;
invMasses[invMasses.Length - 2] = 0.1f;
}
else
{
invMasses[0] = 0.1f;
invMasses[1] = 0.1f;
}
PushDataToSolver(ParticleData.INV_MASSES);
}
else
{
// If it's grabbed then I fixed the particles by putting their masses to 0. Then I change their positions according to the position of the object using SetParticlePositions.
if (reverseLookDirection)
{
invMasses[invMasses.Length - 1] = 0;
invMasses[invMasses.Length - 2] = 0;
Oni.SetParticlePositions(Solver.OniSolver, new Vector4[] { obj.transform.position }, 1, particleIndices[particleIndices.Length - 1]);
Oni.SetParticlePositions(Solver.OniSolver, new Vector4[] { obj.transform.position + (obj.transform.forward * -0.02f) }, 1, particleIndices[particleIndices.Length - 2]);
}
else
{
invMasses[0] = 0;
invMasses[1] = 0;
Oni.SetParticlePositions(Solver.OniSolver, new Vector4[] { obj.transform.position }, 1, particleIndices[0]);
Oni.SetParticlePositions(Solver.OniSolver, new Vector4[] { obj.transform.position + (obj.transform.forward * -0.02f) }, 1, particleIndices[1]);
}
PushDataToSolver(ParticleData.INV_MASSES);
}
}
I hope it's going to be helpful if someone wants to do the same . Please ask if you have any question about the code or how I did it.