Search Unity

Question How can i shorten this script with single if statement ?

Discussion in 'Editor & General Support' started by Lupinder, Dec 1, 2022.

  1. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    if (ProMouse.LeftButton.IsPressed || ProMouse.RightButton.IsPressed || Input.GetKey(KeyCode.Q) && grapplingRope.Grappling)
    {

    I tried like that, it looked right at first but it was not working like i wanted it. After separated codes, it was working correctly but it is just too long. Did i miss something ?

    Code (CSharp):
    1.   private void FixedUpdate()
    2.         {
    3.  
    4.             if (ProMouse.LeftButton.IsPressed  && grapplingRope.Grappling)
    5.             {
    6.  
    7.               hook.transform.position = _hit;
    8.  
    9.                 grappleHolder.rotation = Quaternion.Slerp(grappleHolder.rotation, Quaternion.LookRotation(-(grappleHolder.position - _hit)), rotationSmooth * Time.fixedDeltaTime);
    10.  
    11.  
    12.  
    13.  
    14.                 var distance = Vector3.Distance(player.transform.position, _hit);
    15.                 if (!(distance >= minPhysicsDistance) || !(distance <= maxPhysicsDistance)) return;
    16.  
    17.                 if(timer >= 0.004006f) {
    18.                 timer = 0f;
    19.  
    20.                 player.playerRigidBody.velocity += pullForce * Time.fixedDeltaTime * yMultiplier * Mathf.Abs(_hit.y - player.transform.position.y) * (_hit - player.transform.position).normalized;
    21.                 player.playerRigidBody.velocity += pushForce * Time.fixedDeltaTime * player.transform.forward;
    22.  
    23.     }
    24.             }
    25.  
    26.             else
    27.             {
    28.                 grappleHolder.localRotation = Quaternion.Slerp(grappleHolder.localRotation, Quaternion.Euler(0, 0, 0), rotationSmooth * Time.fixedDeltaTime);
    29.             }
    30.             if (ProMouse.RightButton.IsPressed  && grapplingRope.Grappling)
    31.             {
    32.  
    33.  
    34.    
    35.                 hook.transform.position = _hit;
    36.  
    37.                 grappleHolder.rotation = Quaternion.Slerp(grappleHolder.rotation, Quaternion.LookRotation(-(grappleHolder.position - _hit)), rotationSmooth * Time.fixedDeltaTime);
    38.  
    39.  
    40.  
    41.  
    42.                 var distance = Vector3.Distance(player.transform.position, _hit);
    43.                 if (!(distance >= minPhysicsDistance) || !(distance <= maxPhysicsDistance)) return;
    44.  
    45.                 if(timer >= 0.004006f) {
    46.                 timer = 0f;
    47.  
    48.                 player.playerRigidBody.velocity += pullForce * Time.fixedDeltaTime * yMultiplier * Mathf.Abs(_hit.y - player.transform.position.y) * (_hit - player.transform.position).normalized;
    49.                 player.playerRigidBody.velocity += pushForce * Time.fixedDeltaTime * player.transform.forward;
    50.  
    51.             }
    52.             }
    53.  
    54.             else
    55.             {
    56.                 grappleHolder.localRotation = Quaternion.Slerp(grappleHolder.localRotation, Quaternion.Euler(0, 0, 0), rotationSmooth * Time.fixedDeltaTime);
    57.             }
    58.  
    59.             if ( Input.GetKey(KeyCode.Q) && grapplingRope.Grappling)
    60.             {
    61.  
    62.  
    63.                 hook.transform.position = _hit;
    64.  
    65.                 grappleHolder.rotation = Quaternion.Slerp(grappleHolder.rotation, Quaternion.LookRotation(-(grappleHolder.position - _hit)), rotationSmooth * Time.fixedDeltaTime);
    66.  
    67.  
    68.  
    69.                 var distance = Vector3.Distance(player.transform.position, _hit);
    70.                 if (!(distance >= minPhysicsDistance) || !(distance <= maxPhysicsDistance)) return;
    71.  
    72.                 if(timer >= 0.004006f) {
    73.                 timer = 0f;
    74.  
    75.                 player.playerRigidBody.velocity += pullForce * Time.fixedDeltaTime * yMultiplier * Mathf.Abs(_hit.y - player.transform.position.y) * (_hit - player.transform.position).normalized;
    76.                 player.playerRigidBody.velocity += pushForce * Time.fixedDeltaTime * player.transform.forward;
    77.  
    78.           }
    79.             }
    80.  
    81.             else
    82.             {
    83.                 grappleHolder.localRotation = Quaternion.Slerp(grappleHolder.localRotation, Quaternion.Euler(0, 0, 0), rotationSmooth * Time.fixedDeltaTime);
    84.             }
    85.           }
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    721
    The problem is that the || operator has lower priority than the && operator.

    (A || B || C && D) means "If A, or B, or C and D". It will be true if A is true, or if B is true, or if both C and D are true.

    You need to group the first three parts together with parentheses:

    ((A || B || C) && D)

    This is true if D is true and any of A, B, or C are true.
     
  3. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    Thank you very much. İt worked.
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    721
    Another solution that would improve readability would be to split things up:

    Code (CSharp):
    1. bool pressed = isLeftMousePressed || isRightMousePressed [...]
    2.  
    3. if (pressed && grapplingRope.Grappling) {
    4.   [...]
    5. }
    This makes it easier to see what the code is actually doing!
     
    spiney199 and Kurt-Dekker like this.