x


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

more ▼

asked Aug 24 '11 at 05:45 PM

SimonLewis gravatar image

SimonLewis
1 1 2 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

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) {
        ...
    }
}
more ▼

answered Aug 24 '11 at 05:54 PM

Rennat gravatar image

Rennat
664 5 8 18

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.

Aug 24 '11 at 07:34 PM Rennat
(comments are locked)
10|3000 characters needed characters left

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 :)

more ▼

answered Aug 24 '11 at 06:00 PM

SimonLewis gravatar image

SimonLewis
1 1 2 3

Please use the add new comment button to add a comment to a question or answer instead of using the answer field below. Answers aren't sorted by date so they are hard to use as a conversation.

Aug 24 '11 at 07:32 PM Rennat
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x60
x40

asked: Aug 24 '11 at 05:45 PM

Seen: 644 times

Last Updated: Aug 24 '11 at 07:34 PM