Hello community,
I hope you can give an hint for my problem. I try to detect an collision between two objects and undo the last movement when touched.
-
One object is (half) static (only instant teleports every 5 Minutes), attached:
-
Sphere Collider
-
the other object is my Main Camera, attached:
-
Sphere Collider (Is Trigger = true)
-
Rigidbody (Is Kinematic = true)
-
Script (GetKeyState for movement & OnTriggerEnter) [Code below]
It “works” but the movement is very twitching, because it goes into the collider and back outside until the user notice it, and stop pressing the key
Is there a smooth way how to do that in unity?
Thanks in advance, I’m going crazy.
private bool colliderEntered = false;
// Check collision
void OnTriggerEnter(Collider other)
{
colliderEntered = true;
}
void OnTriggerExit(Collider other)
{
colliderEntered = false;
}
//-----------------------------------------------------------
void Update()
{
if(Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
if (!colliderEntered) // no collision move forward & maybe into the collider!
{
transform.position += transform.forward * speed * Time.deltaTime;
}
else // you stuck in the collider move out
{
transform.position -= transform.forward * speed * Time.deltaTime;
}
lastMove = LastMove.foreward;
}
}
// also left, right, backwards, up and down
}