- Home /
Script is only working when slowing down timescale
I have a round based game with a player and an object called "check" that jumps to the position where the player is going (when an arrow key is pressed) and checks if it is going to collide with something. However it only works properly when I slow down the timescale to 0.01 or put in some waitForEndOfFrames.
This is my the code I use:
function Update(){ if (Input.GetKeyDown(KeyCode.LeftArrow) && spinning == false && left.active == true && scaling == false && !paused) { check.position.x = transform.position.x - transform.localScale.y-20; //moves Check to where the player will be standing after moving Move(Vector3.left); //moves the player } else if (Input.GetKeyDown(KeyCode.RightArrow) && spinning == false && right.active == true && scaling == false && !paused) { check.position.x = transform.position.x + transform.localScale.y+20; Move(Vector3.right); } else if (Input.GetKeyDown(KeyCode.UpArrow) && spinning == false && top.active == true && scaling == false && !paused) { check.position.z = transform.position.z + transform.localScale.y+20; Move(Vector3.forward); } else if (Input.GetKeyDown(KeyCode.DownArrow) && spinning == false && down.active == true && scaling == false && !paused) { check.position.z = transform.position.z - transform.localScale.y-20; Move(Vector3.back); } }
function Move(direction : Vector3) { deactivateColliders(); //disables all colliders of check children oldScale = transform.localScale.y; newScale = oldScale - 20; //player can go one field less after each turn //yield WaitForEndOfFrame(); //yield WaitForEndOfFrame(); activateCheck(); //enables all children of check activateColliders(); // enables their colliders again //code for moving the player... }
Check has four children. One for each direction (left, right, top, down). If one of the children hits a wall it disables itself.
"Check" checks where the player will be able to move in the next round.
The grey tiles are the children of check. As you can see the left one is disabled even if the player should be able to move there.
This problem does not occur when I uncomment the yields or slow down the timescale. I think there might be a problem with the timing. But pretty rarely it works fine. I hope that you understand my problem and have some ideas to solve it somehow. Thanks!
It sounds like a problem with how you're checking for walls/obstacles. Would you $$anonymous$$d posting that code?