Rotation Quaternion Problem

Hi,
Im watching a Project Survival Shooter (Nightmare) and i want to my character rotate using the MouseX and MouseY, And I’ve wrote a code that make it works but it has a problem!
when i rotate my player using mouse position its rotate and if you don’t move your mouse the player back into his original rotation value!!!
Here is the code:

void turning2()
    {
        float x = Input.GetAxisRaw ("Mouse X");
        float y = Input.GetAxisRaw ("Mouse Y");
        rotation.Set (x, 0, y);
        Quaternion newRotate = Quaternion.LookRotation (rotation);
        playerRigidbody.MoveRotation (newRotate);
    }

Actully the main thing that I’ve change the project is i want to make this game to work with Joystick, so please help me to make my player turns and stay on its rotation value
Thank you

Well, I’d say the code is doing exactly what you’ve told it to do — set the rotation to look at whichever way you press the joystick.

If you only want the player to turn in the direction of the joystick when the joystick is pressed in some direction, then you just need to define a “dead zone” within which you don’t muck with the rotation at all. For example:

1 Like

THANK YOU SO MUCH, IT WORKED VERY WELL
But sorry for asking, I’m new to coding.
Can you Explain what these thing do? or send me an link from somewhere to study?
if (Mathf.Abs(x) > 0.3f || Mathf.Abs(y) > 0.3f)

It’s a conditional check to make sure that either the absolute value of x OR the absolute value of y is greater than 0.3. This is so that it avoids rotating the character when you move you joystick a tiny bit.

void turning2()
{
     float x = Input.GetAxisRaw ("Mouse X");
     float y = Input.GetAxisRaw ("Mouse Y");
    
     //if ( (absolute Value of x > 0.3f) OR (absolute Value of y > 0.3) )
     if (Mathf.Abs(x) > 0.3f || Mathf.Abs(y) > 0.3f)
     {
          //Then do the following
          rotation.Set (x, 0, y);
          Quaternion newRotate = Quaternion.LookRotation (rotation);
          playerRigidbody.MoveRotation (newRotate);
     }
}

I’d highly recommend you to look at if/else statements and conditions.
I’d also recommend you to look at true false logic and truth tables.

//Note || mean OR
//Note && means AND
// == is a conditional to check if equal to

if (condition == true)
{
    then do something
}

if (condition1 == true || condition2 == true)
{
   then do something
}

//You can also use math operators >=, >, ==, <=, <
if (x >= 5 || y <= 5)
{
   then do something
}


edit:

  • Don’t worry about the Unknown in the diagram above.
  • Added more to the code
1 Like

Thank you Mixtrate, I appreciate it.