Search Unity

How can ı work my code with another button ?

Discussion in 'Editor & General Support' started by Lupinder, Jan 4, 2021.

  1. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    İn my code when i press left click on my mouse, my code is working perfectly. Now ı want to add a button so this time if ı press right click, code will work. How can ı do this ? Animator paramaters will be different. New one will be "swing" instead "swin". Thanks for help.

    Code (CSharp):
    1.  private void FixedUpdate()
    2.         {
    3.  
    4.  
    5.             if (ProMouse.LeftButton.IsPressed && grapplingRope.Grappling)
    6.             {
    7.  
    8.  
    9.                 grappleHolder.rotation = Quaternion.Lerp(grappleHolder.rotation, Quaternion.LookRotation(-(grappleHolder.position - _hit)), rotationSmooth * Time.fixedDeltaTime);
    10.  
    11.  
    12.                 var distance = Vector3.Distance(player.transform.position, _hit);
    13.                 if (!(distance >= minPhysicsDistance) || !(distance <= maxPhysicsDistance)) return;
    14.  
    15.                 if(timer >= 0.0063f) {
    16.                 timer = 0f;
    17.  
    18.                 player.playerRigidBody.velocity += pullForce * Time.fixedDeltaTime * yMultiplier * Mathf.Abs(_hit.y - player.transform.position.y) * (_hit - player.transform.position).normalized;
    19.                 player.playerRigidBody.velocity += pushForce * Time.fixedDeltaTime * player.transform.forward;
    20.  
    21.  
    22. }
    23.             }
    24.             else
    25.             {
    26.                 grappleHolder.localRotation = Quaternion.Lerp(grappleHolder.localRotation, Quaternion.Euler(0, 0, 0), rotationSmooth * Time.fixedDeltaTime);
    27.             }
    28.         }
    29.  
    30.  
    31.         private void LateUpdate()
    32.         {
    33.            
    34.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("jump"))
    35.             {
    36.                 cameraFov.SetCameraFov(NORMAL_FOV);
    37.                 speedLinesParticleSystem.Play();
    38.  
    39.             }
    40.  
    41.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("Front Flip"))
    42.             {
    43.                 cameraFov.SetCameraFov(NORMAL_FOV);
    44.                 speedLinesParticleSystem.Play();
    45.  
    46.             }
    47.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("Backflip"))
    48.             {
    49.                 cameraFov.SetCameraFov(HOOKSHOT_FOV);
    50.                 speedLinesParticleSystem.Play();
    51.  
    52.             }
    53.  
    54.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("RunningJump"))
    55.             {
    56.                 cameraFov.SetCameraFov(NORMAL_FOV);
    57.                 speedLinesParticleSystem.Play();
    58.  
    59.             }
    60.  
    61.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("İdle"))
    62.  
    63.             {
    64.                 cameraFov.SetCameraFov(NORMAL_FOV);
    65.                 speedLinesParticleSystem.Stop();
    66.  
    67.             }
    68.  
    69.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("Crouch İdle"))
    70.             {
    71.                 cameraFov.SetCameraFov(CROUCH_FOV);
    72.  
    73.             }
    74.  
    75.             if (ProMouse.LeftButton.IsDown && RaycastAll(out var hitInfo))
    76.             {
    77.  
    78.                 if (Input.GetButtonDown("Fire1"))
    79.                 {
    80.                     audioo.Play();
    81.  
    82.                 }
    83.                 cameraFov.SetCameraFov(HOOKSHOT_FOV);
    84.  
    85.  
    86.  
    87.                     speedLinesParticleSystem.Play();
    88.                 otherAnimator.SetBool("Swin", true);
    89.  
    90.  
    91.  
    92.                 grapplingRope.Grapple(grappleTip.position, hitInfo.point);
    93.                 _hit = hitInfo.point;
    94.  
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
    101.             }
    102.  
    103.  
    104.             if (ProMouse.LeftButton.IsUp)
    105.             {
    106.                 cameraFov.SetCameraFov(NORMAL_FOV);
    107.                 speedLinesParticleSystem.Stop();
    108.                 otherAnimator.SetBool("Swin", false);
    109.  
    110.  
    111.                 grapplingRope.UnGrapple();
    112.  
    113.             }
    114.  
    115.          
    116.  
    117.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("AirAnimation"))
    118.             {
    119.                 audioo.Stop();
    120.                 otherAnimator.SetBool("Swin", false);
    121.                 speedLinesParticleSystem.Play();
    122.                 cameraFov.SetCameraFov(SWİN3_FOV);
    123.                 grapplingRope.UnGrapple();
    124.  
    125.             }
    126.  
    127.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("BackFlip"))
    128.             {
    129.                 audioo.Stop();
    130.                 otherAnimator.SetBool("Swin", false);
    131.  
    132.                 grapplingRope.UnGrapple();
    133.  
    134.             }
    135.          
    136.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("Scene"))
    137.             {
    138.                 audioo.Stop();
    139.                 otherAnimator.SetBool("Swin", false);
    140.  
    141.                 grapplingRope.UnGrapple();
    142.  
    143.             }
    144.  
    145.  
    146.  
    147.  
    148.  
    149.             if (this.otherAnimator.GetCurrentAnimatorStateInfo(0).IsName("gap"))
    150.             {
    151.                 audioo.Stop();
    152.                 otherAnimator.SetBool("Swin", false);
    153.  
    154.                 grapplingRope.UnGrapple();
    155.  
    156.             }
    157.             if (ProMouse.LeftButton.IsPressed && grapplingRope.Grappling)
    158.             {
    159.  
    160.  
    161.  
    162.                 otherAnimator.SetBool("Swin", true);
    163.  
    164.                 grapplingRope.UpdateStart(grappleTip.position);
    165.  
    166.  
    167.  
    168.  
    169.  
    170.  
    171.  
    172.             }
    173.             grapplingRope.UpdateGrapple();
    174.  
    175.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Looks like you're using some ProMouse wrapper class... you are going to have to understand how to get at other buttons in that class, unless you want to rewrite to use vanilla Unity input, either the old Input Manager or the new Input 2.0 package.

    If you want to stick with ProMouse, I'd suggest starting with their API documentation.
     
    Joe-Censored likes this.
  3. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    İt will be ProMouse.LeftButton.IsPressed. But how can ı keep them together. Not just left click, at same time ı want to activate my script. Just animator bool will be different. Can you give me suggestion about that ?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I'm not sure what you mean by this? You mean detect only when both are simultaneously pressed?

    The traditional way to do this, since the two buttons will almost never go down in the same frame, is to process your input and set state variables that you act on a short while later.

    For instance: left click... wait for 0.1 seconds if there is no matching right-click, then do a left-click behavior

    if during the 0.1 seconds you ALSO get a right click while the left click is still down, the do two-finger click.
     
    Joe-Censored likes this.
  5. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    ı want to add new if statement like if (ProMouseRightButton.IsPressed && grapplingRope.Grappling). This time it will work when ı press right click. How can ı keep them together. İ will change "swin" bool in second if statement.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Restructure the code so that you read the button and if it is clicked, set a boolean that is otherwise false.

    Then based on your grappling state, decide how to behave, something like:

    Code (csharp):
    1. if (clicked)
    2. {
    3.   if (Grapping)
    4.   {
    5.     // do grappling click
    6.   }
    7.   else
    8.   {
    9.     // do non-grappling click
    10.   }
    11. }
    This centralizes the collection of user intention in case you want to change that in the future, then it won't introduce odd bugs later.
     
    Joe-Censored likes this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I have no idea what this question means. But one potential problem I see in your code is you are checking ProMouse.LeftButton.IsPressed inside FixedUpdate. If IsPressed is only true on the frame where the mouse button is initially pressed, instead of true the entire time it is held down, then you are going to frequently just miss mouse button presses. I don't have the ProMouse documentation, so don't know how IsPressed differs from IsDown.

    But as @Kurt-Dekker says, separate your input checking code from your doing something code. Do your input checks, set bools based on them, then check the state of those bools when you want to do something with that input. It makes it a lot easier when you need to decide to do something based on multiple inputs which may or may not come in on different frames.
     
  8. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    ı solved the problem. Thanks.
     
    Joe-Censored likes this.
  9. Lupinder

    Lupinder

    Joined:
    Oct 6, 2018
    Posts:
    85
    I solved the problem. Thank you.