- Home /
Dash towards cursor
Hello! I want to make something where, if the player presses a button, they dash in the direction of the mouse, into the mouse and, of the mouse pointer is outside of a certain range, the player moves as far as the max range. I did a lot of research on this but all of the discussions are very incomplete. Note: my game is a 2d top-down game with combat mechanics based of dota and league of legends
Answer by Smurfj3 · Dec 17, 2019 at 02:26 AM
That is possible by getting the mouse position and translating it to worldspace. This link will explain you in detail how to do that:
At that point you will have the mouse position and then all you need to do is move the player to that position.
Thank you! Do you know how i can limit the player's maximum distance?
@AugmentedDeveloper try Vector3.Clamp$$anonymous$$agnitude
like this
//Populate these
Vector3 playerPosition;
Vector3 mousePosition;
float maxDistance;
Vector3 moveVector = Vector3.Clamp$$anonymous$$agnitude(mousePositon - playerPosition, maxDistance);
Glad you got it working and somebody else already showed you how to clamp the distance.
Your answer
Follow this Question
Related Questions
ex2d with physics ? 1 Answer
2D Directional top down movement,Topdown 2d Directional Movement 0 Answers
Sprites not recognized as whole-spinning in different directions 0 Answers
2D Geometry Dash Clone Player Stuck Between Tiles 1 Answer
2D Ball Kicking, similar to "Sports Heads", or "1on1 Soccer" -1 Answers