Okay so I’m trying to figure out how to detect a mouse swipe?
Could anyone write an example code so I can learn and understand it, please?
Okay so I’m trying to figure out how to detect a mouse swipe?
Could anyone write an example code so I can learn and understand it, please?
Hi.
I took this out of code I use to drag camera with mouse. I’m learning Unity too, so there might be better ways.
if(Input.GetMouseButtonDown(0)) // check If left mousebutton is down
mouseLastFramePosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
// Gets mouse position in screen.
isDragging = true;
}
if (isDragging){
Vector3 mouseMovement = Camera.main.ScreenToViewportPoint(Input.mousePosition) - mouseLastFramePosition;
// calculates how much mouse have moved between this and last frame
mouseLastFramePosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
// Stores mouse position for use in next frame
}
So mouseMovement variable would be used what ever you want to do with swipe. For example move someting:
objectToMove.tranform.Translate(mouseMovement, Space.World );`
if you want to move only in one axis:
objectToMove.tranform.Translate(mouseMovement.x, Space.World );`
Hope this helps
[edit] Put those inside update