- Home /
Ideas on how to check if mouse moved in a specific shape
I am making a game where the player has to do some "spells". These spells are made by drawing shapes with the mouse. I have an object that is put on a canvas and moves with the mouse clicking, holding and dragging. It has a trail renderer. The problem is that I don't really know how to know if the player did any spell correctly. Spells are symbols that the player has to draw. For example, drawing the ">" sign top to bottom, triggers an attack spell. I tought that I could detect the angle given by
float alpha = Vector2.Angle(lastPoint - currentPoint, Vector2.right)
// lastPoint is the point from where the mouse started
// currentPoint is the point where the mouse is now
then check:
Mathf.abs(alpha - lastAlpha) > 10
// lastAlpha is 0 at the beggining then when this becomes true:
lastAlpha = alpha
to detect when the direction was changed. Then, according on some length and x/y distance criteria I could detect if any of the spells was made. But this doesn't really work. I tought that I could save spells as an object:
class Spell {
public string name;
Vector2[] directions;
}
So for example ">" is { name: "attack", directions: [ new Vector2(40, 20), new Vector2(-40, 20) ] }, meaning that you have to go at least 40 to the right and 20 down, approximately half of x on y and then at least 40 left and 20 down, approximatley half of x on y.
I know this seems hard but maybe there is an easy solution. I would like any help even if it means rethinking the whole system. Thank you.
Answer by b1gry4n · Jun 08, 2018 at 10:08 PM
just spitballin here... if you were to put an "invisible" grid in front of the camera and had the mouse activate/deactivate certain grid squares you could then create spell templates that you could compare against.
so in this example the user missed 2 grid squares of the spell and made 3 errors. you could do a check to allow some error wiggle room. if(missedCount) < 3 && userErrorCount < 3)
or even compare against your "spell book" to find the spell that closest fits the user input shape. i accidentally drew the user input/compare charts off by a grid space, but i think the idea comes across still
Your answer
![](https://koobas.hobune.stream/wayback/20220612164405im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Breaking from a Loop 2 Answers
How to create effects that encase objects? 0 Answers
StatusEffect System - While Loop Being Skipped Over and Cancelled 0 Answers
Cooldown Spells and Icons 0 Answers
Best approach for spell book and spell bar (using composition) 0 Answers