Search Unity

(Beginner) Help with Slow Motion

Discussion in 'Getting Started' started by dcarneiro, Nov 11, 2016.

  1. dcarneiro

    dcarneiro

    Joined:
    Nov 9, 2016
    Posts:
    1
    Hi! I just started learning Unity and C#(I am an illustrator, never programmed before) this week!
    I am loving it, it's hard to sleep now... thinking about gamedesign/codes! :D

    So I started with a plataform 2D game, just to learn basic stuff.
    I made a ball that runs automatically and when hits the wall, switch direction. Also worked on Jump with click. Ok..

    I am trying now to add a SlowMotion when the player is Holding Space. Worked ok, but.. it seems to affect the physics and the trajectory of the jump is changed:
    Red line = normal trajectory
    Blue line = slowmotion trajectory after Space is pressed.
    slowmotion.jpg
    (Link of the img if the attach doesn't wok: https://www.dropbox.com/s/8h7gp8qyhgp3r7d/Captura de tela 2016-11-11 13.14.56.png?dl=0)

    Here is the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HeroController : MonoBehaviour {
    5.  
    6.  
    7.     Rigidbody2D myBody;
    8.     public float jumpHeight = 1700f;
    9.     public float moveSpeed = 350f;
    10.  
    11.     public bool moveRight;
    12.  
    13.     public Transform groundCheck;
    14.     public float groundCheckRadius;
    15.     public LayerMask whatIsGround;
    16.     private bool grounded;
    17.  
    18.     // Mudar Direção
    19.     public Transform wallCheck;
    20.     public float wallCheckRadius;
    21.     public LayerMask whatIsWall;
    22.     public bool hittingWall;
    23.  
    24.     public float slowMotionSpeed = 0.2f;
    25.  
    26.  
    27.     private bool doubleJumped;
    28.  
    29.     void Start () {
    30.    
    31.         myBody = GetComponent<Rigidbody2D> ();
    32.  
    33.     }
    34.  
    35.     void FixedUpdate(){
    36.  
    37.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
    38.         hittingWall = Physics2D.OverlapCircle (wallCheck.position, wallCheckRadius, whatIsWall);
    39.     }
    40.  
    41.     void Update () {
    42.  
    43.         if (hittingWall){
    44.             moveRight = !moveRight;
    45.  
    46.         }
    47.  
    48.         if (moveRight) {
    49.             transform.localScale = new Vector3 (-1f, 1f, 1f);
    50.             myBody.velocity = new Vector2 (-moveSpeed * Time.deltaTime, myBody.velocity.y);
    51.  
    52.         } else {
    53.             transform.localScale = new Vector3 (1f, 1f, 1f);
    54.             myBody.velocity = new Vector2 (moveSpeed * Time.deltaTime, myBody.velocity.y);
    55.  
    56.         }
    57.  
    58.  
    59.  
    60.         if (grounded) {
    61.             doubleJumped = false;
    62.         }
    63.  
    64.         if (Input.GetMouseButtonDown(0) && grounded) {
    65.             myBody.velocity = new Vector2 (myBody.velocity.x, jumpHeight);
    66.         }
    67.            
    68.         if (Input.GetMouseButtonDown(0) && !doubleJumped && !grounded) {
    69.             myBody.velocity = new Vector2 (myBody.velocity.x, jumpHeight);
    70.             doubleJumped = true;
    71.         }
    72.  
    73.         // Test SlowMotion
    74.         if (Input.GetKey (KeyCode.Space)) {
    75.             Time.timeScale = slowMotionSpeed;
    76.             // Time.fixedDeltaTime = slowMotionSpeed * 0.02f;
    77.            
    78.         } else {
    79.             Time.timeScale = 1;
    80.         }
    81.  
    82.  
    83.     }
    84. }
    85.  
    I read somewhere about trying to use the Time.fixedDeltaTime to solve the problem, but I couldn't make it work.

    Anyone can help?
    Is there a better way to create this SlowMotion?
    The idea is that it affects all the game (hero, enemies, moving objects, etc...)

    Thank you!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, there is a better way: simply change Time.timeScale. This will change the time scale in the physics engine, as well as what is reported to you with Time.deltaTime.

    And yeah, it sounds like you've discovered the joy of coding. Welcome to our world! A very interesting and exciting world it is, too. Just one piece of advice: turn around and acknowledge the family now and then. Name tags for the kids are a good idea, too.