Search Unity

Making it possible that time.timeScale affects CharacterController

Discussion in 'Scripting' started by xamur, Jun 22, 2017.

  1. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Hello folks,
    I am currently prototyping game mechanics and needed a first person character controller. Before that I've used the default rigidbody controller from the standard assets.
    I've found a quite good one and everything works as it should, except one important thing. The new character controller uses the Character Controller component instead of rigidbody, what means that it cannot be affected by timeScale (as I have read in a different topic).

    Do you have an idea how I can achieve it that timeScale affects my new controller like the default rigidbody one? I hope it is possible somehow, because I need it.

    Here is the script of the new controller:

    ---
     
    Last edited: Jun 23, 2017
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Are you sure it doesn't already? I see that it's using Time.deltaTime for movement, which is already affected by Time.timeScale.
     
  3. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Yes, I am.
    I have something like a selection wheel and if I hold Tab the timeScale will be set to zero. Releasing tab will set it to 1 again. This worked with the standard assets controller but not while using the one with character controller and the script above.

    Code of the Tab-Wheel (important part):

    ---

    I also have another script which gives the player the ability to control time. Not working either.

    By the way, could you tell me where it uses deltaTime? :p
     
    Last edited: Jun 23, 2017
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    A quick ctrl+f would show you line 309 ;)
    Code (CSharp):
    1. ch.Move (direction *Time.deltaTime);
    If you want to be sure about what values are being used, try printing out what Time.timeScale is just before this line using Debug.Log(Time.timeScale). Or follow the logic into the CharacterController Move function, and keep logging values to see why it still moves.
     
  5. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Oh, of course. Sorry for the noob question. ^^

    I tried that already. As I thought it is always 1 and if I press Tab, it changes to 0. But I can still move my mouse.
    I have to mention something. It works, as I realized to change the timeScale. My other script (where player can change timeScale) affects the time and the movement is slower than before. So far so good!

    But why can I still move the camera with the new controller, but not with the old one (using the same script!):

    Code of the Tab-Wheel (important part again):

    ---
     
    Last edited: Jun 23, 2017
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Moving the mouse and looking around are handled differently.

    In that controller script, check out the function Look():
    Code (CSharp):
    1. void Look()
    2.     {
    3.         mouseX = Input.GetAxis ("Mouse X");
    4.         mouseY = Input.GetAxis ("Mouse Y");
    5.         chRot = transform.eulerAngles;
    6.         camRot = cam.localEulerAngles;
    7.         chRot.y += mouseX * mouseSens;
    8.         camRot.x += -mouseY * mouseSens;
    9.         if (camRot.x > mouseLimmit.x && camRot.x < 180)
    10.             camRot.x = mouseLimmit.x;
    11.         if(camRot.x < mouseLimmit.y && camRot.x > 180)
    12.             camRot.x = mouseLimmit.y;
    13.      
    14.         transform.eulerAngles = chRot;
    15.         cam.localEulerAngles = camRot;
    16.     }
    Not a mention of timeScale or deltaTime, right?

    To stop your controller from looking around, you need to multiply in timeScale somewhere.
    Here perhaps:
    Code (CSharp):
    1. chRot.y += mouseX * mouseSens * Time.timeScale;
    2. camRot.x += -mouseY * mouseSens * Time.timeScale;
     
    xamur likes this.
  7. MilKID

    MilKID

    Joined:
    Jan 8, 2017
    Posts:
    16
    If you want the time to effect on your mouse sensitivity you'll have to calculate it in, I am pretty sure your other script just calculated it differently.

    Edit: Basically what @jeffreyschoch said.. - my reply was just to slow ;-)
     
    xamur likes this.
  8. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Thank you very much, both of you. It works now! :)

    But I found another "problem". My character jumps lower and faster (in timescale = 0.25) than in timescale 1. I tried to implement "* Time.deltaTime" to any possible part of the jump code, but I didn't get it to work.

    ---
     
    Last edited: Jun 23, 2017
  9. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    That is probably because the gravity is still at full power. Did you try multiplying gravityDelta by Time.deltaTime or Time.timeScale?

    Keep in mind when you multiply something by Time.deltaTime, it effectively converts it to "units per second", so you may need to tweak the original value to get it to behave the same as before.
     
    xamur likes this.
  10. xamur

    xamur

    Joined:
    Mar 27, 2014
    Posts:
    109
    Cool, problem solved. Thank you very much for your help! :)
     
    LiterallyJeff likes this.