Search Unity

How do i create a FirstPersonalController?

Discussion in 'Scripting' started by Nanior, Aug 15, 2019.

  1. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    I have always a bug on a FPC Controller, All things is good but just i can rotate my Camera to up 180∘
    And that i don't like, Can you give a sample of FirstPersonalController?:)
    What i now have created:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FPC_Controller : MonoBehaviour
    4. {
    5.     public GameObject Camera;
    6.     public float Sensetivity = 8f;
    7.  
    8.     void Start()
    9.     {
    10.         Cursor.lockState = CursorLockMode.Locked;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         float MouseX = Input.GetAxis("Mouse X");
    16.         float MouseY = Input.GetAxis("Mouse Y");
    17.         transform.Rotate(new Vector3(0, MouseX * Sensetivity * Time.deltaTime * 10, 0));
    18.         if(Camera.transform.rotation.x >= -47 && Camera.transform.rotation.x <= 60)
    19.         {
    20.             Camera.transform.Rotate(new Vector3(-MouseY * Sensetivity * Time.deltaTime * 10, 0, 0));
    21.         }
    22.     }
    23. }
    24.  
    but still don't work:(
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You're having common rookie rotation issues. First, rotation.x and so on are not what you'd initially expect; rotation is a Quaternion, which has wxyz values from -1 to 1; these numbers are not at all the same as the numbers you'd see in the inspector, which are the Euler values.

    But you shouldn't use those either because Euler angles are terrible. I think the linked article will provide you a good understanding of how to work with rotations in Unity and has some examples you should be able to use to make your code work the way you want.
     
    Nanior likes this.
  3. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    I dont really understand...
     
  4. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    OK, got it, Thanks!
     
  5. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Oops, now i got another problem... if my rotation is higher than -47 or lower than 60 than i cant rotate up anymore...
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'd need to see your current code, but I'm assuming you're still doing some variation of:
    Code (csharp):
    1. if (x >= -47 && x < 60) {
    So, if your rotation value is outside of this, your rotation code isn't running at all, and you get stuck.
    The easiest way to fix this is not to have that if statement at all, but rather, to use Mathf.Clamp on your rotation after you move it. The mouse can move the rotation value wherever it wants, but then Clamp will get it back into your desired range.
     
    Nanior likes this.
  7. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Oh, thanks!, i will try it out!
     
  8. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    How do I use mathf.clamp?
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Something like this:
    Code (csharp):
    1. x= Mathf.Clamp(x, -47f, 60f)
     
    Nanior likes this.
  10. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    Thanks!I will see if it works!
     
  11. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    :confused:You mean this?:
    Code (CSharp):
    1. using UnityEngine;
    2. public class FPC_Controller : MonoBehaviour
    3. {
    4.     public GameObject Camera;
    5.     public float Sensetivity = 8f;
    6.     void Start()
    7.     {
    8.         Cursor.lockState = CursorLockMode.Locked;
    9.     }
    10.     void Update()
    11.     {
    12.         float MouseX = Input.GetAxis("Mouse X");
    13.         float MouseY = Input.GetAxis("Mouse Y");
    14.         transform.Rotate(new Vector3(0, MouseX * Sensetivity * Time.deltaTime * 10, 0);
    15.        
    16.         Camera.transform.Rotate(new Vector3(Mathf.Clamp(-MouseY * Sensetivity * Time.deltaTime * 10,-47,60), 0,0));
    17.        
    18.     }
    19. }
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That'll clamp the amount you can rotate each frame, which doesn't really help you. What you need to do is keep track of your own yaw and pitch and apply them afresh each frame, rather than altering the existing rotation.
    Code (csharp):
    1. float yaw=0f;
    2. float pitch=0f;
    3. void Update() {
    4.         float MouseX = Input.GetAxis("Mouse X");
    5.         float MouseY = Input.GetAxis("Mouse Y");
    6.         yaw += MouseX * Sensetivity * Time.deltaTime * 10;
    7. pitch -= MouseY * Sensetivity * Time.deltaTime * 10;
    8. pitch = Mathf.Clamp(pitch,-47,60);
    9.         Camera.transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
    10. }
     
    Nanior likes this.
  13. Nanior

    Nanior

    Joined:
    May 4, 2019
    Posts:
    101
    :confused::confused::confused::(Well, now my camera can't move X