- Home /
Directional melee system
Hi guys, I have a rather simple question. Is it possible to make a directional melee system in unity by "cutting" the screen in 4 pieces so that you can detect in which part your mouse is and through this play different animations based by the position of your mouse on the screen.Of course the weapon would have a collider so i could create defense and attack poses/animations from different directions abit similair to games as war of the roses and mount and blade warband(of course not so advanced). Also is there a better way to do this?
Answer by Baste · May 16, 2015 at 01:32 PM
Pretty easy. You have the variables Screen.width and Sceen.height which gives you your current screen width and height in pixels. Then you have Input.mousePosition, which gives you a vector describing where your mouse is on the screen.
So you could do something like this:
float screenHorizontalMidpoint = Screen.width / 2f;
float screenVerticalMidpoint = Screen.height / 2f;
Vector2 mousePosition = Input.mousePosition;
bool mouseOnTheLeft = mousePosition.x < screenHorizontalMidpoint;
bool mouseOnTheBottom = mousePosition.y < screenVerticalMidpoint;
And use those bools to decide on what attack to do.
Your answer
![](https://koobas.hobune.stream/wayback/20220613195733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AddRelativeForce to another object 1 Answer
How to change velocity directly to a certain speed while keeping the collide direction? 1 Answer
Move Object in direction of mouse? 1 Answer
Objects rotate but remain on the original axis... 0 Answers
Change the direction of velocity upon tapping a button!!?? 1 Answer