Hello everyone!
First some context: I am trying to make kinematic rigidbodys collide with certain colliders.
Here is some code to start getting to my problem.
private void FixedUpdate()
{
AvoidCollision();
}
in the AvoidCollision function i am checking some stuff, but the relevant part is that i am checking the distance between a grasped kinematic object and a collider it is supposed to collide with. When a certain minDistance is reached i call a coroutine which does the following:
private IEnumerator WaitForAction()
{
currentSphere.GetComponent().constraints = RigidbodyConstraints.FreezePosition;
yield return new WaitUntil(() => currentSphere.GetComponent().isGrasped == false);
UnfreezeAndMove();
}
and
private void UnfreezeAndMove()
{
currentSphere.GetComponent().constraints = RigidbodyConstraints.None;
currentSphere.GetComponent().MovePosition(currentSphere.GetComponent().gameObject.transform.position + (Vector3.back * setBackMultiplier));
Debug.Log("hello");
}
The problem is, that when i have the Debug.Log everything works fine. Just as the code is dictating it to.
When i remove the Debug.Log tho, the object which is supposed to be set back on release, is just set back for like one frame(you can see it glitch to its destined position) and then its back at the collider and not able to move when i grab it because of the code. Also the Debug.Log is called 150 times.
I am suspecting some kind of thread problem where the Leap motion update is interfering with the update position of the rigidbody.
i have also tried the following:
void Start(){
InteractionManager.instance.OnPrePhysicsUpdate += UnfreezeAndMove;
}
where
private void UnfreezeAndMove()
{
if (!nowExecute) return;
currentSphere.GetComponentInParent().constraints = RigidbodyConstraints.None;
currentSphere.GetComponentInParent().MovePosition(
currentSphere.GetComponentInParent().gameObject.transform.position + (Vector3.back * setBack));
nowExecute = false;
}
with yield
yield return new WaitUntil(() => currentSphere.GetComponentInParent().isGrasped == false);
nowExecute = true;
But updating it in prePhysics didnt help either.
Does anyone maybe have a suggestion where the problem lies?