How to load scene after 2 seonds

I want when the ball collides with Loose collider, the game over scene should load after 2 sec from the triggering time. How to do that ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoseCollider : MonoBehaviour
{
[SerializeField] AudioClip looseSound;
private void OnTriggerEnter2D(Collider2D collision)
{
AudioSource.PlayClipAtPoint(looseSound, Camera.main.transform.position);
SceneManager.LoadScene(“Game Over”);

}

}

Hi Sibam,

Unity’s physic engine is highly optimised meaning it checks the position of a collider occasionally only. You could try to set the Collision Detection mode of the ball’s Rigidbody2D to “Continuous”. In most cases, that fixes the problem.

In our particular case, the problem is that we manipulate the transform.position via code and override the calculated values of the physics engine. It is likely that the position of the ball does not match the position of the collider anymore.

A better solution would be to disable the physics simulation for the ball while manipulating the position via code.

void Start ()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
    myAudioSource = GetComponent<AudioSource>();
    myRigidBody2D = GetComponent<Rigidbody2D>();
    myRigidBody2D.simulated = false; // -------- add this
}
 
private void LaunchOnMouseClick()
{
    if (Input.GetMouseButtonDown(0))
    {
        hasStarted = true;
        myRigidBody2D.velocity = new Vector2(xPush, yPush);
        myRigidBody2D.simulated = true; // -------- add this
    }
}

Did this help? :slight_smile:

It shows errors. This is the script of my ball. I have to add the above to this, right ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{
[SerializeField] Paddle paddle1;
[SerializeField] float xPush = 2f;
[SerializeField] float yPush = 15f;
[SerializeField] AudioClip ballSound;

//State
Vector2 paddleToBallVector;
bool hasStarted = false;
//Cached component reference
AudioSource myAudioSource;
// Start is called before the first frame update
void Start()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
    myAudioSource = GetComponent<AudioSource>();

}

// Update is called once per frame
void Update()
{
    if (!hasStarted)
    {
        LockBallToPaddle();
        LaunchOnMouseClick();
    }
   
}

private void LaunchOnMouseClick()
{

    if (Input.GetMouseButtonDown(0))
    {
        hasStarted = true;
        GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);
        
    }
}

private void LockBallToPaddle()
{
    Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    transform.position = paddlePos + paddleToBallVector;
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (hasStarted)
    {
        AudioClip clip = ballSound[UnityEngine.Random.Range(0, ballSound.Length)];
        myAudioSource.PlayOneShot(clip);
    }
}

}

What errors does “it” show? And what do you mean by “it”?

It means the code you gave. And error I’m getting is this

After doing this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{
[SerializeField] Paddle paddle1;
[SerializeField] float xPush = 2f;
[SerializeField] float yPush = 15f;
[SerializeField] AudioClip ballSound;

//State
Vector2 paddleToBallVector;
bool hasStarted = false;
//Cached component reference
AudioSource myAudioSource;
// Start is called before the first frame update
void Start()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
    myAudioSource = GetComponent & lt; AudioSource & gt; ();
    myRigidBody2D = GetComponent & lt; Rigidbody2D & gt; ();
    myRigidBody2D.simulated = false; // -------- add this
}

}

// Update is called once per frame
void Update()
{
    if (!hasStarted)
    {
        LockBallToPaddle();
        LaunchOnMouseClick();
    }
   
}

private void LaunchOnMouseClick()
{

if (Input.GetMouseButtonDown(0))
{
    hasStarted = true;
    myRigidBody2D.velocity = new Vector2(xPush, yPush);
    myRigidBody2D.simulated = true; // -------- add this
}

}

private void LockBallToPaddle()
{
    Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
    transform.position = paddlePos + paddleToBallVector;
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (hasStarted)
    {
        AudioClip clip = ballSound[UnityEngine.Random.Range(0, ballSound.Length)];
        myAudioSource.PlayOneShot(clip);
    }
}

}

Those parentheses at the end of each line seem very suspicious, don’t you think? Your compiler even starts its error log by mentioning an issue at those very lines.

But I just copied that code what @Nina gave me.

Try removing them and run it again.

Before adding them only it runs but some errors are showing in the console. Why that’s showing that only I asked.

The forum must have formatted certain characters. Here’s the fix:

void Start ()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
    myAudioSource = GetComponent<AudioSource>();
    myRigidBody2D = GetComponent<Rigidbody2D>();
    myRigidBody2D.simulated = false; // -------- add this
}
 
private void LaunchOnMouseClick()
{
    if (Input.GetMouseButtonDown(0))
    {
        hasStarted = true;
        myRigidBody2D.velocity = new Vector2(xPush, yPush);
        myRigidBody2D.simulated = true; // -------- add this
    }
}

But there’s still an error. It shows, the name myRigidBody2D does not exist in the current context.

Declare a new variable of type Rigidbody2D at the top of your code. Name it myRigidBody2D.

What is at line 46 of your Ball class? It seems that you are calling Rigidbody2D as a class and not its instance, which is myRigidBody2D

So just like your says, you are not using an object reference to set “simulated” to true, but the class itself. Replace Rigidbody2D with myRigidBody2D

1 Like

Thanks man. It solved the error in visual studio.

You are welcome. Mark the answer that helped you as a solution so that our lovely moderators have one less issue on their list : )

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms