I recently added in a very basic “dash” to my player character using the transform.Translate function, and I tried adjusting it because this function obviously makes the dash very rigid and quick. This is the current code:
private float horizontalInput;
private Rigidbody2D rigidbodyComponent;
private bool hasMoved;
private bool facingForward;
[SerializeField] private float dashDistance = 5f;
void Start()
{
rigidbodyComponent = GetComponent<Rigidbody2D>();
facingForward = true;
}
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
if (horizontalInput != 0)
{
hasMoved = true;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if (facingForward)
{
rigidbodyComponent.velocity += Vector2.right * dashDistance;
//transform.Translate(Vector2.right * dashDistance);
}
else
{
rigidbodyComponenet.velocity += -Vector2.right * dashDistance;
//transform.Translate(Vector2.left * dashDistance);
}
}
}
private void FixedUpdate()
{
if (hasMoved)
{
if (horizontalInput > 0.0001)
{
facingForward = true;
}
if (horizontalInput < -0.0001)
{
facingForward = false;
}
}
The commented section of the LeftShift key press is the code I had that worked previously, but was very clunky (especially when checking the facingForward bool). With this new Rigidbody force adjustment, I’m getting very tiny results, as I jump around and move with my character, all the button press seems to do is delay my fall in the air for a moment.
Could this possibly be due to my horizontal player movement? It works like this:
rigidbodyComponent.velocity = new Vector2(horizontalInput * moveSpeed, rigidbodyComponent.velocity.y);
If not, I’m unsure what might be causing this reduction in dash distance compared to my previous method.
I will gladly answer any clarification questions! Thank you for your time reading this post and any help is greatly appreciated!