First of all, thank you for clicking on this!
I can’t get anything to work and I don’t know how to code this.
My project so far:
-2d infinite runner
-character avoids obstacles
-more avoiding = more points
-my first game
What I need:
On the character’s collision with the obstacle, I need the game time to lower, the audio pitch to lower, and after 3 seconds of this slowmotion effect, the level to reset.
Thanks a ton, all I can do is add your name to the credits.
If you face certain problems in your code, you may post it and people will have a look at it. But i doubt anyone’s going to code the whole game for you.
Create a OnCollisionEnter function in your player’s script.
Set Time.timeScale = 0.4 or any other value. This slows down the game, 1.0 is the value for ‘realtime’ gamespeed.
Further information: Unity - Scripting API: Time.timeScale
Additionally, turn down the pitch of the running sounds, you might wanna get all running audio clips first and loop through them and adjust their pitch.
Further information: Unity - Manual: Audio Source
You could try to following code. I’m not quite sure if you would use it in a 2D just as it is, but you’ll get the idea.
void OnCollisionEnter(Collision obj)
{
// check for tag if necessary, i.e. if (obj.gameObject.tag == "yourTag")
AudioSource audio = gameObject.audio; // this should be replaced with your audio sources (if it's an array, loop through it)
Time.timeScale = 0.4f; // slow-sotion effect
audio.pitch = 0.4f;// lowered pitch
StartCoroutine(wait(3f));// starting a coroutine which will wait about 3 seconds till it restarts the game
// you could also implement the timer in update, there are several ways to do it
}
IEnumerator wait(float timeTillRestart)
{
timeTillRestart *= Time.timeScale; // recalculate the seconds so they match realtime-seconds
yield return new WaitForSeconds(timeTillRestart); // function will yield here until 3 seconds have passed
Application.LoadLevel(Application.loadedLevel); // reloading the current level, or another level's index if you want to
}