Search Unity

Question Slopes are causing my rigidbody to gain velocity. How can I prevent this?

Discussion in 'Physics' started by mathiasbc04, Apr 28, 2023.

  1. mathiasbc04

    mathiasbc04

    Joined:
    Dec 21, 2020
    Posts:
    33


    This is my script:
    Code (CSharp):
    1.    private void Update()
    2.     {
    3.         if (Input.touchCount > 0)
    4.         {
    5.             Touch touch = Input.GetTouch(0);
    6.  
    7.             switch (touch.phase)
    8.             {
    9.                 case TouchPhase.Began:
    10.                     startPosition = touch.position;
    11.                     break;
    12.                 case TouchPhase.Moved:
    13.                     // The force on the x axis is calculated...
    14.                     // by how far away the finger is from the starting point
    15.                     xForce = touch.position.x - startPosition.x;
    16.  
    17.                     // Limit x-axis force to prevent advantages from longer screens
    18.                     if (xForce > maxXForce)
    19.                     {
    20.                         xForce = maxXForce;
    21.                     }
    22.                     else if (xForce < -maxXForce)
    23.                     {
    24.                         xForce = -maxXForce;
    25.                     }
    26.                     break;
    27.             }
    28.  
    29.             fingerDown = true;
    30.         }
    31.         else
    32.         {
    33.             fingerDown = false;
    34.         }
    35.     }
    36.  
    37.     private bool OnGround()
    38.     {
    39.         Vector3 rayOrigin = transform.position;
    40.         float rayDistance = 1.0f;
    41.         LayerMask layerMask = LayerMask.GetMask("Ground");
    42.         RaycastHit hit;
    43.         if (Physics.Raycast(rayOrigin, Vector3.down, out hit, rayDistance, layerMask))
    44.         {
    45.             slopeNormal = hit.normal;
    46.             // Calculates the angle along z axis
    47.             slopeAngle = Vector2.Angle(Vector3.up, slopeNormal);
    48.  
    49.             return true;
    50.         }
    51.  
    52.         return false;
    53.     }
    54.  
    55.  
    56.     private void FixedUpdate()
    57.     {
    58.         if (fingerDown)
    59.         {
    60.             // Calculations to make it more responsive
    61.  
    62.             // calculate the direction we want to move in and our desired velocity
    63.             float targetSpeed = (xForce * sideSpeed);
    64.             // calculate the difference between current velocity and desired velocity
    65.             float speedDif = targetSpeed - rb.velocity.x;
    66.             // Change acceleration rate depending on situation
    67.             float accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? acceleration : decceleration;
    68.             // Applies acceleration to speed difference, then raises to a set power so acceleration increases with higher speeds
    69.             // finally multiplies by sign to reapply direction
    70.             float movement = Mathf.Pow(Mathf.Abs(speedDif) * accelRate, velPower) * Mathf.Sign(speedDif);
    71.  
    72.  
    73.             // Making the ball move along a slope
    74.            
    75.             if(OnGround())
    76.             {
    77.                 forceDirection = Vector3.ProjectOnPlane(new Vector3(movement, 0, 0), slopeNormal);
    78.             }
    79.             else
    80.             {
    81.                 forceDirection = new Vector3(movement, 0, 0);
    82.             }
    83.  
    84.             // Makes the ball move from left ot right
    85.             rb.AddForce(new Vector3(forceDirection.x, forceDirection.y, 0));
    86.  
    87.         }
    88.  
    89.         // Makes the ball move forward
    90.         rb.AddForce(Vector3.forward * forwardSpeed * Time.fixedDeltaTime);
    91.     }