Ok, I have a script that if my player touches something to die and just transform his position to the start point. But I want to make a script that when the player collides with the object the player to stay there for a second or two without moving and then transform his position to the start one. Here is the script that i currently have. I tried to find something about Delay but couldn’t implement it in my script.
{
void OnCollisionEnter2D(Collision2D col)
{
if (col.collider.tag == "Sharps")
transform.position = new Vector3(0, 0, 0);
print(transform.position.x);
}
}
You would usually use coroutines to do stuff delayed. They’re methods that you “kick off”, and that can use the yield statement to pause execution of that method. It will run alongside your other scripts until it is done. The way to solve your problem here is:
void OnCollisionEnter2D(Collision2D col)
{
if (col.collider.tag == "Sharps"){
StartCoroutine(MovePlayer());
}
}
IEnumerator MovePlayer() {
yield return new WaitForSeconds(2f);
transform.position = new Vector3(0, 0, 0);
}
Note the yield return new WaitForSeconds statement. That makes the specific method wait for however many second you send in (2 in this case), and then go on with execution. An important point is that you have to start the method with a StartCoroutine method call, and the method itself has to have the IEnumerator return type.