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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Figuring out how to slow down time for a single object on button click.

Discussion in 'Scripting' started by koothur19021, Apr 12, 2015.

  1. koothur19021

    koothur19021

    Joined:
    Mar 14, 2015
    Posts:
    17
    Hello , I am making an android 2d Game and I want to slow down my sphere when button pressed while other objects are on its normal speed doing their work. First of all I tried with Time.timeScale but it is global and effects every object in the world. This is what I came up with at last and seem to work the opposite way , I mean when I press button I can make my sphere to faster but cant make it move slower on click button. This is the script I wrote for button.

    Code (JavaScript):
    1. var character : GameObject;
    2. var buttonMoveSpeed = 0f;
    3. function OnGUI() {
    4.     while (GUI.RepeatButton(new Rect(15, 100, 100, 50), "Button") )
    5. {    print("Button pressed");
    6.     character.transform.Translate(buttonMoveSpeed*Time.deltaTime,0,0);
    7. }
    8. }
    9.  
    and this is the script I wrote for the sphere to move
    Code (JavaScript):
    1. #pragma strict
    2. var speed = 0f;
    3. function Start () {
    4. }
    5. function Update() {
    6. transform.Translate(speed*Time.deltaTime,0,0);
    7. }
    I don't understand what is wrong. If the opposite to slow speed can work why cant slow speed work on button click. I have also noted that when I change the value for slow motion to happen I can see the sphere slows down for a milli second or so but the intensity of the sphere moving is so great that the slowing down cant keep up maybe.

    Any help is appreciated.
    Thnx for your time.
     
  2. Magison

    Magison

    Joined:
    Mar 1, 2013
    Posts:
    21
    Your transform is moved by your Update function. Every Frame.
    If you press the button you ALSO move it with your OnGUI function.
    Both movements will be happend if you press the button. One + the other. So it gets faster.
    Try to do it in OnGUI OR in Update combined with an else statement.
    If button pressed translate slow ELSE translate fast.

    Better solution is to use a boolean for your key and do it in one script attached to you sphere.

    Code (csharp):
    1.  
    2. var buttonMoveSpeed = 10.0f;
    3. var speed = 50.0f;
    4. var buttonPressed = false;
    5.  
    6. function OnGUI() {
    7.      if (GUI.RepeatButton (new Rect(15, 100, 100, 50), "Button") {
    8.          buttonPressed = true;
    9.      } else {
    10.          buttonPressed = false;
    11.      }
    12. }
    13.  
    14. function Update() {
    15.    if (buttonPressed) {
    16.           transform.Translate(buttonMoveSpeed * Time.DeltaTime, 0, 0);
    17.    } else {
    18.           transform.Translate(speed * Time.DeltaTime, 0, 0);
    19.    }
    20. }
    21.  
    22.  
    If you need a separated GUI script for other reasons, just make the 'buttonPressed' boolean static. Then you can access it from any script.

    Note: You should never do things like move an object inside the GUI function.
    The GUI function could be called more than one time per frame by the engine.
    You have no control over it. So you also have no control over the speed at all.
    OnGUI is for GUI elements and to set variables depending on them.
    Movements have to be done in Update (or FixedUpdate if they controlled by physix).
     
    Last edited: Apr 13, 2015