Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

rigidbody velocity issue

Discussion in 'Input System' started by slaga, Feb 23, 2021.

  1. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    Apart from movement which works i cannot script a dash function. i tried it with a context callback which doesnt work but i also tried it with a key press ( wasPressedThisFrame ) i want my player to dash in the direction he is looking at. Now with the old system this code worked fine from idle and running state.
    I'll skip the whole function since by using this line here : rb2d.velocity = transform.right * dashSpeed; does absolutely nothing on either button call or context call.
    I was told that maybe the movement vector messed it up so i temporarily deleted all movement vectors but still it wont work. Here is the movement code just in case you need it!

    Code (CSharp):
    1. public void PlayerDirectionInput(InputAction.CallbackContext context)
    2.     {
    3.         movementVector = context.ReadValue<Vector2>();
    4.  
    5.         if (!freezeInput)
    6.         {
    7.             movementX = movementVector.x;
    8.             movementY = movementVector.y;
    9.             Debug.Log(movementVector);
    10.         }
    11.     }
    and yeah why not here is the messed up dashing script in case anyone can help!
    Code (CSharp):
    1. public void Dashing(InputAction.CallbackContext context)
    2.     {
    3.  
    4.         if ((context.started))// && isGrounded )// && (movementX != 0)) //was (Input.GetButtonDown("Dash") && (keyHorizontal != 0))
    5.         {
    6.             Debug.Log("ButtonPressed (Started)");
    7.             //Instantiate(dashEffect, transform.position, Quaternion.identity);
    8.             isDashing = true;
    9.             //isGrounded = false;
    10.             //currentDashTime = startDashTime;
    11.             ///rb2d.velocity = Vector2.zero;
    12.             rb2d.velocity = transform.right * dashSpeed;
    13.             Debug.Log("dashing " + rb2d.velocity);
    14.  
    15.         }
    16.  
    17.         if (context.performed )//&& isDashing)
    18.         {
    19.             //currentDashTime -= Time.deltaTime;
    20.             Debug.Log("dashing now ( perfomed)" + rb2d.velocity);
    21.             //isDashing = false;
    22.         }
    23.         if (context.canceled)// && currentDashTime <= 0)
    24.         {
    25.             Debug.Log("button released (canceled");
    26.             isDashing = false;
    27.         }
    28.  
    29.  
    30.     }
     
  2. Trystan_bro

    Trystan_bro

    Joined:
    Feb 14, 2021
    Posts:
    1
    I'm not sure if this will help as I don't have too much experience, but have you tried adding the following to your (context.performed) condition?

    rb2d.velocity = transform.right * dashSpeed * currentDashTime;

    The time variable may need to be explicitly added to the vector
     
  3. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246

    keep in mind Player Input Invoke Unity Event behavior only update when the Input values has changed
    context.performed does not function like mono behavior Update() or FixedUpdate(), this is most common mistake when switching to the new Input System, I can tell by saw the Time.deltaTime inside context.performed

    context.performed does not function like mono behavior Update() or FixedUpdate().
     
  4. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    thank you guys but for some reason i figured it out. i set the actual velocity and movement on another function that i call from update and in the input action context function i did it like this.
    Code (CSharp):
    1. public void OnDash(InputAction.CallbackContext context)
    2.     {
    3.         if (context.performed)
    4.         {
    5.             if (context.interaction is TapInteraction)
    6.             {
    7.                 Instantiate(dashEffect, transform.position, Quaternion.identity);
    8.                 Debug.Log("Performed dash");
    9.                 canDash = false;
    10.             }
    11.         }
    12.         if (context.started)
    13.         {
    14.             canDash = true;
    15.         }
    16.         if (context.canceled)
    17.         {
    18.             canDash = false;
    19.         }
    20.     }
    and my dash function


    Code (CSharp):
    1.  public void Dashing()
    2.     {
    3.         if (canDash && isGrounded)//(Input.GetButtonDown("Dash"))// && (keyHorizontal != 0))
    4.         {
    5.             isDashing = true;
    6.             isGrounded = false;
    7.             currentDashTime = startDashTime;
    8.             rb2d.velocity = Vector2.zero;
    9.  
    10.         }
    11.  
    12.         if (isDashing)
    13.         {
    14.             animator.Play("Player_Slide");
    15.             isGrounded = false;
    16.             rb2d.velocity = transform.right * dashSpeed;
    17.             currentDashTime -= Time.deltaTime;
    18.  
    19.             if (currentDashTime <= 0)
    20.             {
    21.                 isDashing = false;
    22.                 isGrounded = true;
    23.             }
    24.         }
    25.  
    26.     }