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);
}
}
}