0
\$\begingroup\$

I am working on a game using GoogleVR, but I don't think that has anything to do with my problem.

Basically, I am testing to see if I can move a stick by using the mouse at the moment.

I am getting mouse movement, as seen from the value of md:

(-0.5,-1.25)
UnityEngine.Debug:Log(Object)
PlayerMovement:Update() (at Assets/Character Pack/PlayerMovement.cs:30)

But the transform is wrong, as seen from the localRotation:

(0,0,0)
UnityEngine.Debug:Log(Object)
PlayerMovement:Update() (at Assets/Character Pack/PlayerMovement.cs:29)

I just want to see my stick move in some predictable way based on the movement of the mouse, and I am not certain why my equation is wrong.

public class PlayerMovement : MonoBehaviour {
    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    public GameObject stick;

    // Use this for initialization
    void Start () {
        smoothV.x = 0.5f;
        smoothV.y = 0.5f;
    }

    // Update is called once per frame
    void Update () {
        var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));

        md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
        smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);

        stick.transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
        stick.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, stick.transform.up);
        Debug.Log ("(" + stick.transform.localRotation.x + "," + stick.transform.localRotation.y + "," + stick.transform.localRotation.z + ")");
        Debug.Log ("(" + md.x + "," + md.y + ")");
    }
}
\$\endgroup\$
1
\$\begingroup\$

There are a lot of mistakes in this code, which makes it a bit difficult to sort out what's the root problem you're trying to solve versus what's noise.

Firstly, you're storing your mouse movement in md, and debugging its contents to make sure it got set properly, but not using it at all for rotation. Your rotation code is using a completely different variable mouselook which so far as I can tell you never populate with any non-zero values.

Then your rotation code sets localRotation with your pitch rotation... then immediately overwrites this with a yaw rotation, so the first line setting localRotation might as well not be there — the very next line replaces whatever it tried to do. Did you mean to combine these two partial rotations using multiplication or Quaternion.Euler?

Your rotation debug only looks at the x, y, and z components of the localRotation quaternion, which won't be very informative. These components hold the axis of rotation, scaled by the sine of half the angle — which is very nearly zero for small angles. To get a more intelligible human-readable value, you might want to check transform.localEulerAngles instead.

Lastly, you need to be a bit careful when rotating on two axes in sequence, as this can produce unexpected results based on the order of composition.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you. I missed mouseLook, but more importantly you were correct that it was horribly wrong. Here is what I am happy with, just to show that it will move. It isn't perfect but it achieves the goal. stick.transform.Rotate(45 * (Input.GetAxis("Mouse X") * RotationSpeed * Time.deltaTime), 0, 45 * (Input.GetAxis("Mouse Y") * RotationSpeed * Time.deltaTime)); \$\endgroup\$ Jul 2 '17 at 5:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.