admin管理员组文章数量:1302431
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
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
Share asked Feb 10 at 21:14 Emilia ZeroEmilia Zero 437 bronze badges1 Answer
Reset to default 2By 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.
本文标签: cHow does Rigidbody2D linearVelocity movement meddle with AddForce2DStack Overflow
版权声明:本文标题:c# - How does Rigidbody2D linearVelocity movement meddle with AddForce2D? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741689874a2392660.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论