- Home /
OnTriggerEnter too slow?
This is my code, and it works.. BUT there is (like always) a flaw.
private bool w = false;
void Update()
{
if (Input.GetKeyDown("w"))
{
w = true;
}
void OnTriggerEnter(Collider otherObject)
{
if (otherObject.tag == "walls-blocks")
{
Debug.Log("(PlayerHitWall) Player hit: " + otherObject.name);
if (w == true)
{
gameObject.transform.position = lastPosition;
}
}
}
As soon as my player collides with a WALL, it gets placed back to it's last position. But, if I rapidly pressed W (moves against the wall) it moves THROUGH the wall after 5 times tapping W. Is this because the update method of OnTriggerEnter is slow? Any idea on how to fix this?
Answer by gabs · Feb 07, 2012 at 02:47 PM
This could be happening because OnTriggerEnter depends on physics, and Unity physics are processed on the fixed update function. So, if you're updating the position in a regular Update function, it's probable that it will run faster than the FixedUpdate function, since it has a variable framerate. Why not use the default collision reaction setup?
Your answer
Follow this Question
Related Questions
Changing the rotation of a Trigger makes the Trigger not work anymore? 1 Answer
physics question my object setup failing collision 3 Answers
Instantiate an Object on Another Object 2 Answers
Tesla Tower Trap 1 Answer
Teleportation Loop issue 1 Answer