0

(This is 3d. Screen shot attached for reference.)

I have read so many articles on here or elsewhere on issues similar to mine but nothing solves it. My pistol is pointing forward and I only rotate on the x and z axis (up , down , left, right) but my pistol always ends up tilted to the left or right. I tried solutions like this (Problems limiting object rotation with Mathf.Clamp()) (substituting x for y)but my gun ends up freezing or in in weird positions. I think I'm missing a fundamental concept here.

I should also mention that the rotating left, right , up down on their own does work correctly BUT its when combining them all together is when things go awry. For example: left rotate > then up rotate > then left rotate then I end up in a titled position

You can see in the picture that my controls on the bottom left only allow the user to move the gun up, down left and right --- but somehow the gun ends up tilted, in this screen shot to the right --- I want to prevent tilt to right or left.

public void Update()
         {
             if (mButtonHeld) {
                 switch(mCurBtnName){
                     case "ButtonUp":
                     mPistol.transform.Rotate (PISTOL_INCREMENT, 0, 0); 

                     break;
                     case "ButtonDown":
                        mPistol.transform.Rotate (-PISTOL_INCREMENT, 0, 0); 

                     break;
                     case "ButtonLeft":
                         mPistol.transform.Rotate(0,0,-PISTOL_INCREMENT_LR);    

                     break;
                     case "ButtonRight":
                         mPistol.transform.Rotate(0,0,PISTOL_INCREMENT_LR);
                     break;
                 }
                         //How can I lock y axis here??
             } 
         }

enter image description here

Community
  • 1
  • 1
Mike6679
  • 5,547
  • 19
  • 63
  • 108
  • 1
    If your gun is aiming along the Z-axis and you rotate it on the Z-axis it is going to tilt. Perhaps you meant to rotate it along the Y-axis instead? – Goibon Aug 12 '15 at 15:06

1 Answers1

1

Applying rotations to different axis is not commutative; the order is important and this is probably what is causing you problems. A simple fix is to do something like that:

public void Start()
{
    (...)
    m_VectorPistolInitState = mPistol.transform.rotation;
}

public void Update()
{
    static float azimuth = 0;
    static float elevation = 0;
    if (mButtonHeld) {
        switch(mCurBtnName){
            case "ButtonUp":
                elevation += PISTOL_INCREMENT;
            break;
            case "ButtonDown":
                elevation -= PISTOL_INCREMENT;
            break;
            case "ButtonLeft":
                azimuth -= PISTOL_INCREMENT_LR;
            break;
            case "ButtonRight":
                azimuth += PISTOL_INCREMENT_LR;
            break;
        }

        mPistol.transform.rotation = m_VectorPistolInitState;
        mPistol.transform.Rotate(elevation,0,azimuth);
    } 
}
Frederic Gaudet
  • 161
  • 1
  • 5
  • Thanks however, that code causes basic left, right, up, down, movements not to work correctly at all. So with your code every-time I tap the up button it increments it more than the previous tap. Also with your code , tapping the left or right button causes the gun to immediately tilt. – Mike6679 Aug 12 '15 at 16:05
  • Edited to add the missing rotation reset. – Frederic Gaudet Aug 12 '15 at 20:01
  • Ok, not quite right yet....when the game first starts, and I press any button to move the gun, the gun jumps to a unexpected position (i.e the gun faces directly to the left). BUT if I then use my buttons to position the gun to the starting position (the gun facing directly forward), THEN your code all works as it should. – Mike6679 Aug 13 '15 at 01:06
  • Edited to keep the initial rotation of the object. – Frederic Gaudet Aug 13 '15 at 02:12
  • I replaced mPistol.transform.rotation = Quaternion.identity; with mPistol.transform.rotation = m_VectorPistolInitState; m_VectorPistolInitState is initialized in Start() with : m_VectorPistolInitState = mPistol.transform.rotation;. If you update your answer, I'll mark as the correct Answer. Thx for the help. – Mike6679 Aug 13 '15 at 02:30