Делаю переключатель на основе If и bool в OnTriggerStay (код ниже) дело в том, что иногда он срабатывает как надо, то есть по очереди выполняет if и else, а иногда может по три раза выполнить if, потом один раз else и наоборот, в общем сбивается. Может кто нибудь сталкивался и знает как решить эту проблему?
Синтаксис:
Используется csharp
public bool handUp = false;
void OnTriggerStay(Collider other)
{
if (other.tag == "hand")
{
handUp = !handUp;
if (Input.GetKeyDown(KeyCode.G))
{
if (handUp == true)
{
obj_Rigidbody.freezeRotation = true;
obj_Rigidbody.constraints = RigidbodyConstraints.FreezePosition;
transform.rotation = new Quaternion(hand.transform.rotation.x, hand.transform.rotation.y,
hand.transform.rotation.z, hand.transform.rotation.w);
transform.position = new Vector3(hand.transform.position.x, -2.2f, hand.transform.position.z);
transform.parent = GameObject.Find("hand").transform;
Debug.Log("1");
}
else
{
obj_Rigidbody.constraints = RigidbodyConstraints.None;
transform.parent = null;
Debug.Log("2");
}
}
}
}
void OnTriggerStay(Collider other)
{
if (other.tag == "hand")
{
handUp = !handUp;
if (Input.GetKeyDown(KeyCode.G))
{
if (handUp == true)
{
obj_Rigidbody.freezeRotation = true;
obj_Rigidbody.constraints = RigidbodyConstraints.FreezePosition;
transform.rotation = new Quaternion(hand.transform.rotation.x, hand.transform.rotation.y,
hand.transform.rotation.z, hand.transform.rotation.w);
transform.position = new Vector3(hand.transform.position.x, -2.2f, hand.transform.position.z);
transform.parent = GameObject.Find("hand").transform;
Debug.Log("1");
}
else
{
obj_Rigidbody.constraints = RigidbodyConstraints.None;
transform.parent = null;
Debug.Log("2");
}
}
}
}