- Home /
Implementing a virtual joystick with mouse
I am trying to implement a virtual joystick with the mouse. It needs to support sweeping gestures like an analog stick does. I feel like if I could force it to center each frame, this would be trivial. Is there a way to do this, or does anybody have suggestions? I tried just using raw mouse input, but it didn't feel intuitive for sweeping gestures.
I'm not sure what mouse mechanic you are looking for, but the first thing I'd try is to set the 'Type' of movement for the 'Horizontal' and 'Vertical' axes to '$$anonymous$$ouse $$anonymous$$ovement' (Edit/Project Settings/Input). Then put this on a cube in a new scene:
#pragma strict
var speed = 1.0;
function Update() {
transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, Input.GetAxis("Vertical") * speed * Time.deltaTime, 0.0);
}
Rather than centering the cursor, it calculates the delta movement every frame.
Sort of. This would work if I was interested in translating with the mouse, or if I was implementing freelook. I'm actually doing something like swinging an axe. On a gamepad, this is accomplished by making a rotating motion with an analog stick. On mouse, however, I can't make this feel natural. I think it is because every frame my reference point changes, unlike an analog stick where the reference point is always the same. If I could fix the mouse to a point, I could easily emulate controler feedback. Does that help?
I've converted my answer to a comment. Why not make your reference point the start of cutting...on the mouse down or at whatever event starts the cutting motion?
That is the whole crux of the problem. I may end up doing that as a last resort, but I was hoping to just have this sit out there passively. $$anonymous$$aybe it will help if I give a little background on the application, it will help. I'm designing an experimental gameplay take on old-school Legend of Zelda. Basically, all user interaction will be analog. Want to swing a sword? Actually input a swing in the controls. Want to fire an arrow? You will have to pull back the draw string. In the sword scenario, I plan to have the sword just sticking out as people walk around, similar to the old Atari game Adventure. I want to passively listen to controler state to correctly orient the sword. On gamepad, this is accomplished by snapping the sword to a vector given by the orientation of the right analog stick. This supports a sweeping/swinging motion since all values are relative to a fixed reference point. If I required a mousedown event, I could do the same thing on mouse, I just wish there was a more passive way to do this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105225im_/https://answers.unity.com/themes/thub/images/avi.jpg)