Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How Do I Use Horizontal Input Along With Other Modifications To Velocity?

Discussion in '2D' started by Spri1te, Aug 5, 2023.

  1. Spri1te

    Spri1te

    Joined:
    Jun 23, 2023
    Posts:
    2
    I'm making a platformer where one of your primary forms of movement is a shoot ability's kick back. The issue is, that kick back shot doesn't work well with the horizontal movement I have in place. Here's the code for the horizontal movement:
    Code (CSharp):
    1. float horizontalInput = Input.GetAxis("Horizontal");
    2.         body.velocity = new Vector2(horizontalInput * moveSpeed, body.velocity.y);
    (body is the Rigidbody2D I have for the player, and this is in the FixedUpdate method) And here's the code I have for the kick back shot:
    Code (CSharp):
    1. private void KickBackShot(Vector3 mousePosition)
    2.     {
    3.         body.velocity = new Vector2(0, 0);
    4.         kickBackDirection = mousePosition - transform.position;
    5.         kickBackDirection = -kickBackDirection;
    6.         kickBackDirection = kickBackDirection.normalized;
    7.         if (IsGrounded() == false)
    8.         {
    9.             body.AddForce(kickBackDirection * kickBackStrength);
    10.         }
    11.         else
    12.         {
    13.             StartCoroutine(DelayedShotDischarge(delaySeconds));
    14.             body.AddForce(kickBackDirection * kickBackStrength);
    15.         }
    16.     }
    (IsGrounded checks whether the player is on the ground or not using a boxcast, DelayedShotDischarge makes it so using the kick back shot on the ground doesn't allow you to use it in midair). The issue is that with the current code, the kick back shot has really jumpy movement, and allows for basically no movement on the x axis. But if I change the movement script to this:
    Code (CSharp):
    1.  
    2.         body.velocity = new Vector2(body.velocity.x + (horizontalInput * moveSpeed), body.velocity.y);
    then if you touch the horizontal movement key, the player zooms in that direction with reckless abandon. Is there a way I can let these things work at the same time?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    This is similar to any "knockback" system.

    The only difference here is that the user's action causes the knockback.

    There are thousands of tutorials already out there for knockback. You might want to start with those

    The simplest knockback setups simply inhibit user input during the knockback time.

    More complicated approaches will blend out the amount of user input so that when the knockback starts there is no user input, but the user input is gradually blended back in, perhaps with Lerp or simple averaging.

    I suggest starting with the barest minimum functionality: inhibit user input for a short time to allow knockback. A simple cooldown timer can suffice:

    Cooldown timers, gun bullet intervals, shot spacing, rate of fire:

    https://forum.unity.com/threads/fire-rate-issues.1026154/#post-6646297
     
  3. Spri1te

    Spri1te

    Joined:
    Jun 23, 2023
    Posts:
    2
    I'll try that, but the kick back isn't really supposed to be a side effect of the "main shot", it's really just supposed to act as a main platforming ability, with the "shooting" being an explanation for why it is an ability that launches the player away from the mouse. I'll try your solution though.