Search Unity

Scripting a First Person Camera is too hard.

Discussion in 'Scripting' started by Deleted User, Jan 18, 2019.

  1. Deleted User

    Deleted User

    Guest

    I've tried to find a video to find out how to make a first person camera but it's just too hard to find a proper one. I tried making my own and this kinda works but it also rotates the Z Axis. Can anyone please use a little bit of their time to make as simple as a First Person Camera script can be?


    Code (CSharp):
    1.         float translation = Input.GetAxis("Mouse Y") * speed;
    2.         float rotation = Input.GetAxis("Mouse X") * rotationSpeed;
    3.         translation *= Time.deltaTime;
    4.         rotation *= Time.deltaTime;
    5.         plrCam.transform.Rotate(translation, rotation, 0);
     
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Your choice of terms is a bit confusing. I think you are looking for "pitch" and "yaw", based on how you are using them. Translation usually means movement in space, like walking forwards.
    As for the logic problem, the problem is that you want to rotate around the world's y-axis and the player camera's x-axis.
    Code (csharp):
    1. float pitch = Input.GetAxis("Mouse Y") * pitchSpeed * Time.deltaTime;
    2. float yaw = Input.GetAxis("Mouse X") * yawSpeed * Time.deltaTime;
    3.  
    4. plrCam.transform.Rotate(pitch, 0, 0, Space.Self);
    5. plrCam.transform.Rotate(0, yaw, 0, Space.World);
    Note that with this, there is no clamping, so you can pitch all the way up and then back, so the camera would be upside down.
     
  3. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Generally you just have to rotate the player on their Y (left/right) axis, then rotate the camera on the X (up/down) axis.

    Just do the example above, but use your player's transform for the yaw rotation, and parent the camera to the player.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Can you imagine how difficult it must have been back in the early 1990s when they were making Wolf3D/Doom/Quake!? They didn't even use real floating point numbers back then!

    And yet they had awesome first person cameras.

    Nobody is going to solve this problem again for you. It's been solved THOUSANDS of times in Unity. There are quite a few freebie first person controllers out there, starting with the Unity Standard Assets and ranging to all kinds of other random ones on github and Unity wiki. Study those, take them apart, wiggle their parts until you understand how they all work, then try and build your own.
     
    Joe-Censored and xVergilx like this.
  5. wikky

    wikky

    Joined:
    Apr 22, 2020
    Posts:
    10
    I can't get the script to show up for some reason to apply it I have the script but its body isn't showing up or anything under the script to apply it if that makes sense, how do I fix this?
     
  6. bananabasher

    bananabasher

    Joined:
    Jun 3, 2020
    Posts:
    2
    Is there a way to script on Mac I can't do it
     
  7. Cubemann

    Cubemann

    Joined:
    Oct 22, 2020
    Posts:
    4
    Watch Brackey's FPS Controller tutorial. Search it up.