#Infinite force applied to object when trying to dash

1 messages · Page 1 of 1 (latest)

muted olive
#

I am trying to implement a dashing function, but a never-ending force is applied when the input is triggered. How do I fix this?

using UnityEngine;
using UnityEngine.InputSystem;

public class CharacterController : MonoBehaviour
{
[SerializeField] private float movementSpeed = 5;
[SerializeField] private float dashForce = 10;

private Rigidbody playerRb; // Ensure you switch to 2d afterwards

private InputSystem_Actions inputSystemActions;

private void Awake()
{        
    playerRb = GetComponent<Rigidbody>();

    inputSystemActions = new InputSystem_Actions();
    inputSystemActions.Player.Enable();
    inputSystemActions.Player.Dash.performed += Dash;
}

private void FixedUpdate()
{
    float input = inputSystemActions.Player.Move.ReadValue<Vector2>().x;
    playerRb.linearVelocity = Vector3.right * input * movementSpeed;
}

private void Dash(InputAction.CallbackContext context)
{
    print("dash");
    var control = context.control;

    if (context.performed && control.name == "a")
    {
        print("a");
        playerRb.AddForce(Vector3.left * dashForce, ForceMode.Impulse);
    }
    else if(context.performed && control.name == "d")
    {
        print("d");
        playerRb.AddForce(Vector3.right * dashForce, ForceMode.Impulse);
    }
}

}

sick stone
#

!code

sonic ploverBOT
sick stone
#

although im not sure that's really a code question

#

you sure you set up the inputs correctly

muted olive
#

pretty certain I did yeah

sick stone
#

i mean given the code it seems like it isn't set up well, tbh

muted olive
#

how could it be better?

sick stone
#

don't hardcode the control names, your code usually shouldn't care about those

#

your code should be thinking in terms of actions and values rather than keypresses

muted olive
#

so just Dash instead of seperate a and d?

sick stone
#

well it'd probably require a bit more redesigning than just that, i don't know what exactly the intended input scheme is

#

i'd guess you may want to separate dash left and right

muted olive
#

ah ok that's useful thanks

sick stone
#

anyways - are you getting multiple a/d logs for pressing a/d once? what type is the dash action?

muted olive
#

although i dont know if that would solve the problem

muted olive
sick stone
sick stone
#

seems your FixedUpdate is actually overriding the force for the dash

#

would help if you could share your code properly btw

muted olive
#

how do I do that>

#

'''cs
using UnityEngine;
using UnityEngine.InputSystem;

public class CharacterController : MonoBehaviour
{
[SerializeField] private float movementSpeed = 5;
[SerializeField] private float dashForce = 10;

private Rigidbody playerRb; // Ensure you switch to 2d afterwards

private InputSystem_Actions inputSystemActions;

private void Awake()
{
    playerRb = GetComponent<Rigidbody>();

    inputSystemActions = new InputSystem_Actions();
    inputSystemActions.Player.Enable();
    inputSystemActions.Player.Dash.performed += Dash;
}

private void FixedUpdate()
{
    float input = inputSystemActions.Player.Move.ReadValue<Vector2>().x;
    playerRb.linearVelocity = Vector3.right * input * movementSpeed;
}

private void Dash(InputAction.CallbackContext context)
{
    print("dash");
    var control = context.control;

    if (context.performed && control.name == "a")
    {
        print("a");
        playerRb.AddForce(Vector3.left * dashForce, ForceMode.Impulse);
    }
    else if(context.performed && control.name == "d")
    {
        print("d");
        playerRb.AddForce(Vector3.right * dashForce, ForceMode.Impulse);
    }
}

}
'''

sick stone
#

backticks, not quotes

muted olive
#
using UnityEngine;
using UnityEngine.InputSystem;

public class CharacterController : MonoBehaviour
{
    [SerializeField] private float movementSpeed = 5;
    [SerializeField] private float dashForce = 10;

    private Rigidbody playerRb; // Ensure you switch to 2d afterwards

    private InputSystem_Actions inputSystemActions;

    private void Awake()
    {        
        playerRb = GetComponent<Rigidbody>();

        inputSystemActions = new InputSystem_Actions();
        inputSystemActions.Player.Enable();
        inputSystemActions.Player.Dash.performed += Dash;
    }

    private void FixedUpdate()
    {
        float input = inputSystemActions.Player.Move.ReadValue<Vector2>().x;
        playerRb.linearVelocity = Vector3.right * input * movementSpeed;
    }

    private void Dash(InputAction.CallbackContext context)
    {
        print("dash");
        var control = context.control;

        if (context.performed && control.name == "a")
        {
            print("a");
            playerRb.AddForce(Vector3.left * dashForce, ForceMode.Impulse);
        }
        else if(context.performed && control.name == "d")
        {
            print("d");
            playerRb.AddForce(Vector3.right * dashForce, ForceMode.Impulse);
        }
    }
}

merry radish
#

could you test your dash with 2 different keys than a and d because i am asusming it overrides the velocity there

#

since you have this in the FixedUpdate float input = inputSystemActions.Player.Move.ReadValue<Vector2>().x;

#

also make sure your dashForce in the Inspector has a corrct value because the value you assign there might have been changed via inspector

muted olive
#

yes that fixed it, thank you

#

how do you think I could have it so i can still control the dash by double tapping a or d?

muted olive
merry radish
#

i´d not disable the velocity, it is not correct approach. maybe use something else for dash , some kind of double tap

muted olive
#

do you mean double tapping another key which is not related to the movement?

sick stone
#

maybe disable normal movement for a bit

#

that's how i have it - you're locked into the dash for some time

#

but i have the dash setting a velocity for the entire duration instead of applying an impulse

merry radish
#

it is up to you how you want to handle it

muted olive
#

Ok thank you for the help