Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Why do two platforms with the same variable move at different speeds?

Discussion in 'Physics' started by Fyrebend, May 23, 2019.

  1. Fyrebend

    Fyrebend

    Joined:
    Apr 28, 2019
    Posts:
    18
    I am currently working on an infinite running side-scroller, but in 3D. I felt to make it easier, I made the player jump up and down, with the platforms moving towards the player. everything runs alright, except now im trying to make a character that can dash, which just speeds up the platforms movement speed for a few frames.

    Here is my ground movement script in my GroundScroll file:
    Code (CSharp):
    1. void Update () {
    2.         float z = ground.position.z;
    3.         ground.position = new Vector3(ground.position.x, ground.position.y, z-(scrollSpeed * Time.deltaTime));
    4.  
    5.         if (scrollSpeed < 40f)
    6.             scrollSpeed += (scrollIncrease * Time.deltaTime);
    7.     }
    and here is my jump/dash function in my PlayerJump file:
    Code (CSharp):
    1. if (jumpsRemaining > 1)
    2.             {
    3.                 player.velocity = new Vector3(0, jumpForce, 0);
    4.                 jumped = true;
    5.                 --jumpsRemaining;
    6.                 IsGrounded = false;
    7.             }
    8.             else if (jumpsRemaining == 1)
    9.             {
    10.                 groundSpeed = GroundScroll.scrollSpeed;
    11.                 --jumpsRemaining;
    12.                 dashing = true;
    13.                 GroundScroll.scrollSpeed *= 7;
    14.             }
    15.  
    and here is my end of dash in the PlayerJump file:
    Code (CSharp):
    1. if (dashing)
    2.         {
    3.             ++frames;
    4.             if(frames < 7)
    5.             {
    6.                 player.GetComponent<MeshCollider>().enabled = false;
    7.                 player.velocity = new Vector3(0, 0, 0);
    8.                 player.GetComponent<Rigidbody>().useGravity = false;
    9.             }
    10.             else
    11.             {
    12.                 player.GetComponent<MeshCollider>().enabled = true;
    13.                 frames = 0;
    14.                 dashing = false;
    15.                 player.GetComponent<Rigidbody>().useGravity = true;
    16.                 GroundScroll.scrollSpeed = groundSpeed;
    17.             }
    18.         }
    The problem is, during the dash, some platforms move faster than others making a gap the player cant get across, and I just can't figure out why. Is there something wrong that I'm doing?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    5,428
    What is the "frames" variable in the last bit of code for? If frames is less than 7, what is GroundScroll.scrollSpeed getting set to?

    Also, where does "scrollIncrease" come from, and why are you multiplying it by Time.deltaTime when adding it to scrollSpeed? The only times I see you use scrollSpeed it is also being multiplied by Time.deltaTime. So you're effectively multiplying scrollIncrease by Time.deltaTime twice. My guess is this is the source of the issue, since if your frame rate isn't perfectly steady the effect of scrollIncrease on setting ground.position is going to vary wildly.

    But the "frames" variable still has me concerned.
     
  3. Fyrebend

    Fyrebend

    Joined:
    Apr 28, 2019
    Posts:
    18
    Frames is just from the dash, and it is sitting inside the update function. Every time update is called, frames increments, and it was how I track how long my dash was. As for groundScroll.scrollSpeed, if you start the dash, it is multiplied by 7, and after 7 frames it is returned to the original speed.

    The scroll increase slowly increases the speed that the ground scrolls at, so the difficulty is raised. I also multiply it by time.deltaTime because I read that keeps it consistent no matter what your frame rate is at.

    You think my problem might be from multiplying by time.deltatime twice, but my dash doesnt modify scroll increase at all, just the groundscroll speed. And wouldn't it still be consistent since I modify the variable, and then all platforms access the same variable from the same file? They dont get modified separately, right?