Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Slow Motion Script (made in C#)

Discussion in 'Scripting' started by theepicdude986, Sep 20, 2014.

  1. theepicdude986

    theepicdude986

    Joined:
    Apr 9, 2014
    Posts:
    3
    Here is a script I made to were if the player presses the 'Z' key, he will be in slow motion for 5 seconds, it will be a little longer than 5 seconds because when the player slows down, the timer slows down with them! You will also be able to deactivate the Slow Motion once again when pressing Z while in slow motion. Slow motion also re-generates over time while not in activated. If you like it or have better methods feel free to let me know. Here's the script,

    ______________________________________________________________________________________


    using UnityEngine;
    using System.Collections;

    public class SlowMotion : MonoBehaviour {

    float currentAmount = 0f;
    float maxAmount = 5f;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    if(Input.GetKeyDown ("z")){

    if(Time.timeScale == 1.0f)
    Time.timeScale = 0.3f;

    else

    Time.timeScale = 1.0f;
    Time.fixedDeltaTime = 0.02f * Time.timeScale;
    }


    if(Time.timeScale == 0.03f){

    currentAmount += Time.deltaTime;
    }

    if(currentAmount > maxAmount){

    currentAmount = 0f;
    Time.timeScale = 1.0f;

    }

    }
    }

    ______________________________________________________________________________________
     
    Last edited: Sep 20, 2014
  2. Unityolks

    Unityolks

    Joined:
    Jul 1, 2017
    Posts:
    3
    thanks, i've been looking for a slow motion script and I'm terrible at programming
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
  4. RedBraken

    RedBraken

    Joined:
    Aug 29, 2017
    Posts:
    1
    Hey, I am just a beginner in programming
    And I was just going through time and framerate management and I got this script:

    Void update()
    {
    If (input.getbuttondown("fire1")
    {
    Time.timescale = time.timeScale / 4;
    }
    If (time.timescale < 0.25)
    Time.timescale = 1;
    }

    I am beginner so if any mistakes , please ask.
     
    KzH99 likes this.
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I recommend if you're a noob go through Brackey's tutorial on the subject, he's been doing a very good job with his more up to date stuff.

     
    iepicgreen_2020 and DevDop like this.
  6. cartersupergames

    cartersupergames

    Joined:
    Aug 13, 2020
    Posts:
    39
    thx the script helped i havent tested it yet but still
     
  7. mohamedsae

    mohamedsae

    Joined:
    Feb 13, 2021
    Posts:
    1
    Thx, i fixe my fps Drop When i use your code, thx
     
  8. IsaacTown

    IsaacTown

    Joined:
    Mar 8, 2021
    Posts:
    3
    My max Time will not do anything and my game will be in Slow Motion forever
     
  9. Spark59op

    Spark59op

    Joined:
    Jan 22, 2021
    Posts:
    3
    Bro i think he made a mistake in line

    If(Time.timeScale == 0.03f)
    Here change 0.03 to 0.3 and thats it...
    This helped me.. Try it out
     
    metaldc4life likes this.
  10. smileatron2000

    smileatron2000

    Joined:
    Aug 7, 2021
    Posts:
    1
    Thanks I'm making a fps game and I needed a gimmick, this is perfect
     
  11. metaldc4life

    metaldc4life

    Joined:
    Sep 26, 2014
    Posts:
    179
    Wow,
    Thank You For This Script!