- Home /
how to get real time mouse position (left / right) ???
am working on a small tank project were i have a moving tank now what am wanting to do is move my turret when i move my mouse left or right code below is what i currently have but it dont work so well can anyone help or show me a better way of detecting when mouse is going left or right ??
function Update () {
if (Input.mousePosition.y > 0) { //Rotate the turret to the right
Debug.Log("Mouse is going Right");
transform.Rotate(Vector3(0,rotationVel * Time.deltaTime,0));
} else if (Input.mousePosition.y < 0) { //Rotate the turret to the left
Debug.Log("Mouse is Going Left");
transform.Rotate(Vector3(0,-rotationVel * Time.deltaTime,0));
}
}
thank alot
Answer by Rennat · Aug 24, 2011 at 05:54 PM
There may be a better way to do this but I would do it like this
private var lastMousePosition : Vector2;
private var mouseDelta : Vector2;
function Start () {
lastMousePosition = Input.mousePosition;
}
function Update () {
mouseDelta = Input.mousePosition - lastMousePosition;
lastMousePosition = Input.mousePosition;
// now mouseDelta represents the change in position since the last frame
if (mouseDelta.x > 0) {
...
}
else if (mouseDelta.x < 0) {
...
}
}
When I posted this I hadn't tested it but after running it I can confirm 100% that mouseDelta
does indeed behave correctly.
Please post your new script so we can see what else may have gone wrong.
Answer by SimonLewis · Aug 24, 2011 at 06:00 PM
thanks for fast reply that dont seam to work also when i move right it says moving right but when i move the mouse to the left it dont seam to pick it up right away thanks for the help :)
Please use the add new comment
button to add a comment to a question or answer ins$$anonymous$$d of using the answer field below. Answers aren't sorted by date so they are hard to use as a conversation.