- Home /
Question by
gvella29 · Nov 23, 2021 at 11:36 AM ·
positionienumeratortimer countdownreset-position
My objects are not moving when collided with player when trying to reset their position after a timer.
My idea is to activate a timer when my obstacles change their position after being hit by the player to reset their position, but when I run that part of the code the obstacles stay in a fixed position and I do not know why.
Can someone help me please ? Thank you :)
public class Obstacles : MonoBehaviour { Rigidbody obstacleRb; Vector3 spawnOGPos; bool fallen;
// Start is called before the first frame update
void Start()
{
fallen = false;
obstacleRb = GetComponent<Rigidbody>();
spawnOGPos = transform.position;
}
// Update is called once per frame
void Update()
{
if (transform.position.y < 0) {
ResetObstaclePosition();
}
if (transform.position != spawnOGPos){
StartCoroutine(ObstacleFallCountdownRoutine());
ResetObstaclePosition();
}
IEnumerator ObstacleFallCountdownRoutine()
{
yield return new WaitForSeconds(5);
fallen = true;
}
}
void ResetObstaclePosition() {
//Resetting attirbutes
obstacleRb.velocity = Vector3.zero;
obstacleRb.angularVelocity = Vector3.zero;
obstacleRb.rotation = Quaternion.identity;
transform.position = spawnOGPos;
fallen = false;
}
}
Comment