Search Unity

Problem with bouncy platforms

Discussion in 'Physics' started by MayoNinjaGames, Dec 17, 2018.

  1. MayoNinjaGames

    MayoNinjaGames

    Joined:
    Nov 7, 2018
    Posts:
    31
    So I've come far into creating my first Unity game, but I'm having some problem with bouncy platforms.

    If my player hits two springs in a row, the player will sort of just teleport a great distance as shown in the gif here (It starts at around 7secs):



    I've set bounciness to 50 and I was just messing with the friction setting to see if it changes anything.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    We'll probably need to see some code. From your video, I assume you're doing something interesting when hitting those springs. I wounldn't think you could get that kind of bound action purely from a PhysicMaterial's bounciness. Are you calling AddForce at some point when the player hits a spring?

    Also, I don't think bounciness values greater than 1 have any meaning, or aren't any bouncier than 1.
     
  3. Deleted User

    Deleted User

    Guest

    Yeah from the look of it there is some kind of "teleportation" occurring on the first spring as well, so it would be a good idea to take a closer look at the code behind those springs.

    Yep: https://docs.unity3d.com/ScriptReference/PhysicMaterial-bounciness.html

    PS: Cut your gifs rather than leaving 7 seconds in where nothing happens, if your gif capture software doesn't allow that, I'd suggest ScreenToGif.exe, a free little program that has all the gif related functionalities, editing included.
     
  4. MayoNinjaGames

    MayoNinjaGames

    Joined:
    Nov 7, 2018
    Posts:
    31
    I don't have any special code in the Spring class, except activating the animation on collision, but I have this as movement in my player class:

    Code (CSharp):
    1. void movement()
    2.     {
    3.         GetComponent<Rigidbody2D>().velocity = Vector2.ClampMagnitude(GetComponent<Rigidbody2D>().velocity, maxVelocity);
    4.  
    5.         pcInput();
    6.         mobileInput();
    7.        
    8.  
    9.         //Set RBs position
    10.         GetComponent<Rigidbody2D>().position = (new Vector2(GetComponent<Rigidbody2D>().position.x + moveXSpeed, GetComponent<Rigidbody2D>().position.y));
    11.  
    12.         //Give player points depending on how far the player has moved
    13.         points += moveXSpeed / 10;
    14.  
    15.     }
    I have a feeling the issue is in the ClampMagnitude line. My main goal was to limit the height of the players jump.
     
  5. MayoNinjaGames

    MayoNinjaGames

    Joined:
    Nov 7, 2018
    Posts:
    31
    If I remove the ClampMagnitude, the problem is gone, but the player keeps gaining height every time he jumps on a platform now. Any other way to limit the height? Should I just use AddForce instead on contact with a other collider?
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    You haven't shown any code, so I really have no idea how you're performing the bounce. Show the logic that fires when the player his the jump.
     
  7. MayoNinjaGames

    MayoNinjaGames

    Joined:
    Nov 7, 2018
    Posts:
    31
    This is it:

    Code (CSharp):
    1. void movement()
    2.     {
    3.  
    4.         pcInput();
    5.         mobileInput();
    6.  
    7.  
    8.         //Set RBs position
    9.         GetComponent<Rigidbody2D>().velocity = Vector2.ClampMagnitude(GetComponent<Rigidbody2D>().velocity, maxVelocity);
    10.         GetComponent<Rigidbody2D>().position = (new Vector2(GetComponent<Rigidbody2D>().position.x + moveXSpeed, GetComponent<Rigidbody2D>().position.y));
    11.  
    12.         //Give player points depending on how far the player has moved
    13.         points += moveXSpeed / 10;
    14.  
    15.     }
    Settings on the player:



    And the ground has a rigidbody with the bouncy material (set to 10, no friction) and the spring has bouncy material (set to 25, no friction). Both with box colliders. All components in 2D. :)

    EDIT: And PC Input is simply:

    Code (CSharp):
    1. void pcInput()
    2.     {
    3.         //Right button input = increase speed
    4.         if (Input.GetButton("Right"))
    5.         {
    6.             moveXSpeed += 0.2f * Time.deltaTime;
    7.  
    8.             if (moveXSpeed > maxMoveXSpeed) moveXSpeed = maxMoveXSpeed;
    9.         }
    10.  
    11.         //Left button input = decrease speed
    12.         if (Input.GetButton("Left"))
    13.         {
    14.             moveXSpeed -= 0.2f * Time.deltaTime;
    15.  
    16.             if (minimumMoveSpeed > moveXSpeed) moveXSpeed = minimumMoveSpeed;
    17.         }
    18.     }