Search Unity

how to jump at the same speed while fluxuating timecale

Discussion in 'Scripting' started by smallville7123, Dec 1, 2018.

  1. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    im trying to make a game where the player can slow down time at will but remain in normal speed (like superman or the flash :p) and im having trouble getting the jumping physics correct, i tried using the code from http://answers.unity.com/answers/857994/view.html via

            public float JumpVelocity;
    public float JumpDampening = 0.1f;

    void Jump()
    {
    JumpVelocity = 0.5f;
    m_RigidBody.useGravity = false;
    }

    // Update is called once per frame
    private void Update()
    {
    GroundCheck();
    InteractRaycast();
    // printfTools.Tools.fprintf(Debug.Log, "controller.isGrounded = %s, m_IsGrounded = %s", controller.isGrounded == true ? "true" : "false", m_IsGrounded == true ? "true" : "false");
    if (controller.isGrounded)
    {
    moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= MovementSpeed;
    if (Input.GetButton("Jump")) Jump();
    // printfTools.Tools.fprintf(Debug.Log, "moveDirection.y = jumpForce ( %G ) / TM.Matrix_Time ( %G ) = %G (%G)", jumpForce, TM.Matrix_Time, jumpForce / TM.Matrix_Time, moveDirection.y);
    MovementSpeed = MovementSpeedDefault / TM.Matrix_Time;
    }
    verticalVelocity = moveDirection.y;
    controller.Move(moveDirection * Time.fixedDeltaTime);
    Vector3 pos = transform.position;

    if (JumpVelocity != 0)
    {
    pos.y += JumpVelocity;
    JumpVelocity -= JumpDampening;
    if (JumpVelocity <= 0)
    {
    m_RigidBody.useGravity = true;
    JumpVelocity = 0;
    }
    }
    else
    {
    pos.y -= JumpDampening;
    }

    transform.position = pos;
    }


    but my character just no-clips through everything including the ground thus i cannot check if this is accurate and solves my problem or not as it just keeps falling and falling, however it seems to fall at the same speed regardless of whether it is slowed down or not which is what i want (as your perception of time should appear to not change until you actually compare yourself to an object that is affected by time thus to yourself you would not notice that you are falling faster or slower or moving faster or slower untill again you compare yourself to something which is not affected by time)


    my previous code was

            // Update is called once per frame
    private void Update()
    {
    GroundCheck();
    InteractRaycast();
    // printfTools.Tools.fprintf(Debug.Log, "controller.isGrounded = %s, m_IsGrounded = %s", controller.isGrounded == true ? "true" : "false", m_IsGrounded == true ? "true" : "false");
    if (controller.isGrounded)
    {
    moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= MovementSpeed;
    if (Input.GetButton("Jump")) moveDirection.y = jumpForce / TM.Matrix_Time;
    // printfTools.Tools.fprintf(Debug.Log, "moveDirection.y = jumpForce ( %G ) / TM.Matrix_Time ( %G ) = %G (%G)", jumpForce, TM.Matrix_Time, jumpForce / TM.Matrix_Time, moveDirection.y);
    MovementSpeed = MovementSpeedDefault / TM.Matrix_Time;
    }
    else
    {
    moveDirection.y -= (9.8f * Time.fixedUnscaledDeltaTime) / TM.Matrix_Time;
    printfTools.Tools.fprintf(Debug.Log, "moveDirection.y = (9.8f * Time.fixedUnscaledDeltaTime ( %G )) ( %G ) / TM.Matrix_Time ( %G ) = %G (%G)", Time.fixedUnscaledDeltaTime, (9.8f * Time.fixedUnscaledDeltaTime), TM.Matrix_Time, (9.8f * Time.fixedUnscaledDeltaTime) / TM.Matrix_Time, moveDirection.y);
    }
    verticalVelocity = moveDirection.y;
    controller.Move(moveDirection * Time.fixedDeltaTime);
    }


    wich works however is not accurate, for example,


    if you slow down then jump then speed back up you end up jumping like 10 times higher
    if you jump than slow down i think you jump shorter, im not sure
    if you slow down you fall faster than you normally would
    if you slow down, jump, allow yourself to fall, speed backup while falling, then you fall faster than you normally would

    again this is dependant on Time.TimeScale


    at the moment i can only rely on debug printf's and visual observation as i cannot time the jump times for differences, eg


    time it takes from stand still on ground to peak of jump,
    time it takes from peak of jump to stand still on ground again
     
    Last edited: Dec 2, 2018
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    You have

    Position += gravity * time...

    Acceleration is a square function: 9.8 is your DERIVATIVE. You need to integrate.
     
  3. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    im not sure i understand, like... Position += gravity * time... would be for acceleration right? if so wouldn't the deceleration speed cancel out the acceleration instantly?
     
    Last edited: Dec 2, 2018
  4. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I misread your code, I see you are altering a velocity so the 9.8 is your derivative.

    What’s happening then is due to the discrete nature that you have your time variable and parabolas need smooth maths. You’ll still have to integrate, but there is an easier way to do it.

    I had a similar problem and I solved it by precalculating it’s trajectory, then just adjusted position based on the time since it left it’s initital position. Since you’ve done the integration in the formulas, it’s continuous and smooth.
     
  5. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    and how would that work with a fluctuating timescale, such as rapidly increasing and decreasing Time.TimeScale to random positive and negative numbers
     
  6. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    as it needs to be normalized so it acts the same in all cases and appears unaffected (doesnt need to be perfect)
     
  7. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    if i do
                    if (Input.GetButton("Jump")) moveDirection.y += ((9.8f * 30f) * Time.fixedUnscaledDeltaTime) / TM.Matrix_Time;
    it still acts akwardly when speeding back up, presumably cus gravity is decreasing wich is making it jump higher
     
  8. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    so i need to look at this a different way, how can i increase the speed of a jump without increasing its jump hight nor jump force nor gravity
     
  9. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    http://answers.unity.com/answers/857994/view.html if i attempt to use this like

    public float JumpVelocity;
    public float JumpDampening = 0.1f;

    void Jump()
    {
    JumpVelocity = 0.5f;
    m_RigidBody.useGravity = false;
    }

    void FixedUpdate()
    {
    Vector3 pos = transform.position;

    if (JumpVelocity != 0)
    {
    pos.y += JumpVelocity;
    JumpVelocity -= JumpDampening;
    if (JumpVelocity <= 0)
    {
    m_RigidBody.useGravity = true;
    JumpVelocity = 0;
    }
    }

    transform.position = pos;
    }

    // Update is called once per frame
    private void Update()
    {
    GroundCheck();
    InteractRaycast();
    // printfTools.Tools.fprintf(Debug.Log, "controller.isGrounded = %s, m_IsGrounded = %s", controller.isGrounded == true ? "true" : "false", m_IsGrounded == true ? "true" : "false");
    if (controller.isGrounded)
    {
    moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= MovementSpeed;
    if (Input.GetButton("Jump")) Jump();
    // printfTools.Tools.fprintf(Debug.Log, "moveDirection.y = jumpForce ( %G ) / TM.Matrix_Time ( %G ) = %G (%G)", jumpForce, TM.Matrix_Time, jumpForce / TM.Matrix_Time, moveDirection.y);
    MovementSpeed = MovementSpeedDefault / TM.Matrix_Time;
    }
    verticalVelocity = moveDirection.y;
    controller.Move(moveDirection * Time.fixedDeltaTime);
    }

    private Vector2 GetInput()
    {
    Vector2 input = new Vector2
    {
    x = CrossPlatformInputManager.GetAxis("Horizontal"),
    y = CrossPlatformInputManager.GetAxis("Vertical")
    };
    return input;
    }



    than my player just sits there with Jump Velocity equal to 0
     
  10. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    HOWEVER it does seem unafected by the timescale tho i cannot fully test this as it basically clips through the ground
     
  11. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    rephrased my question
     
  12. smallville7123

    smallville7123

    Joined:
    Nov 30, 2018
    Posts:
    35
    fk, i cannot use Time.FixedDeltaTime and Time.fixedTime as if i put an explosion next to a cube while slowed down the cube physics appear to be lagging alot, like 0.5 fps wich is bad, i need the physics to be realtime but still slowed down with the player