I was trying to add a dash mechanics to my 2D game(for a player to dash towards a mouse). Final dash command looks like this:
Vector2 dashDirection = (mousePosition - rb.position).normalized;
rb.linearVelocity = dashDirection * dashSpeed;
But when I tested it only jumped up via Y axis. I did some googling and the only lead I've found is that player movement mechanic can meddle with this, but couldn't find any specific info of how does it affect dashes and ext. My player movement script consists of determining the direction character is facing, and then concludes with something like this:
rb.linearVelocity = new Vector2(moveDir * movementSpeed, rb.linearVelocity.y);
Dash function is called every time player mouseclicks, while movement is checked in FixedUpdate
By analyzing the situation what I understand is that you are trying to add a dash mechanic to the game. Taking in context the mousePosition is calculated as intended and gives the correct values i.e. the world coordinates. Now addressing the problem, according to you the player is only moving in the Y direction, this is caused due to you modifying the linearVelocity on two separate locations. The X direction of the dash is not calculated properly as it is being set to moveDir * moveSpeed. At the time of dash the player's X linearVelocity is updated for one frame and then set back to 0 the next as the moveDir X is also zero (there is no input in X direction). In order to solve this issue you will need to take the dot product of the moveDir vector and the dash Direction vector. Another alternate solution is to take the √(moveDir.x + linearVelocity.x). This will solve the issue of it not travelling in X axis. Anyone of the solution can work but I recommend taking the dot product of the dashDirection and the moveDir as it will also remove the unnecessary assignment of linearVelocity.y to the linearVelocity. Hope that answers the question. Please inform me if the problem has been solved. I apologise in advance if I couldn't understand the problem to the maximum as I am answering from my mobile. Reply to this to address any further issues.