I am trying to track the mouse movement of the mouse as it moves up and down on the screen. i can find out its location on the screen by using Vector3 ray = Input.mousePosition;. however i cannot come up with a method that tells me when the mouse is going up and when it is going down. any help will be appreciated
Try this:
private var ray : Vector3;
private var lastRay : Vector3 = Vector3.up;
private var mouseVelocity : Vector3;
private var dir : String;
var movementSensitivity : float = 0.5;
function Update() {
ray = Input.mousePosition;
mouseVelocity = ray - lastRay;
if (mouseVelocity.y>movementSensitivity) {
dir = "Up";
} else if (mouseVelocity.y<-movementSensitivity) {
dir = "Down";
} else {
dir = "None";
}
print("Mouse Velocity Vector: "+mouseVelocity+", Movement Y direction: "+dir);
lastRay = ray;
}
Basically, it just records the ray each frame, and updates the lastRay to the last mouse position. We subtract to see the direction of movement between frames. Then based on the mouseSensitivity variable, you can choose to register a certain level of movement per frame as an “Up” or “Down”.
A more precise approach might somehow average between multiple frames to get a “general” direction of motion, as opposed to a per-frame direction as we did here.
Hope this is what you’re looking for! I’m new to Unity, so this is just based on some limited programming experience, but feel free to ask for clarification.
Thanks i will have a go at that. I am gonna try convert it to c# as that is what i am coding it in at the moment. you don’t possibly have a c# version do you?
wait a minute i can easily convert this to c# you don’t need to convert it to c#