Search Unity

First Person Controller

Discussion in 'Scripting' started by Jofnir_IceSesh, Jan 27, 2017.

Thread Status:
Not open for further replies.
  1. Jofnir_IceSesh

    Jofnir_IceSesh

    Joined:
    Jan 27, 2017
    Posts:
    72
    Hey guys. So, I've had Unity for a bit now and I've mainly been focusing on 2D with C#. I now want to do some things in 3D. I plan on just making a 3D world that I can start from the ground up as I learn more. First and foremost, is the First Person Controller. I do not want to use the asset. I've tried understanding it, but there's just too much for me to really grasp all at once. I want to start with something simple, and expand.

    So far I have this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     public float lookX;
    9.     public float lookY;
    10.     public float Speed = 3f;
    11.     public Camera cam;
    12.  
    13.     void Start()
    14.     {
    15.  
    16.     }
    17.     void Update()
    18.     {
    19.         lookX = Input.GetAxis ("Mouse X") * Speed;
    20.         lookY = Input.GetAxis ("Mouse Y") * Speed;
    21.  
    22.         Vector3 rot = new Vector3 (lookX, lookY, 0);
    23.         cam.transform.Rotate (rot);
    24.  
    25.     }
    26. }
    I want to get the mouse look going first, as the actual movement seems to be the hardest part. The variables do impact the cameras rotation, but moving the mouse in play mode doesn't change them. Can I get some help/tips?

    UPDATE:

    I got it to move with the mouse by putting the axis' in the update function.. oops. But now its backswards and doesnt really work too well. I'm sure I'm making a simple mistake somewhere.
     
    Last edited: Jan 27, 2017
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You should probably study up on 3D maths and rotations if you plan to make a first person shooter. For starters, to look up and down (pitch), you rotate around the X axis. To look left and right (yaw), you rotate around the Y axis. So you have your axes backwards.

    Secondly, you should probably rebuild the rotation every frame using Quaternion.Euler rather than using Rotate to avoid incremental gains and it'll also be easier to clamp the rotations so you don't roll over on yourself.
     
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,815
    Why re-invent the wheel?

    Unity standard assets has a 1st person controller script.
    After importing the Standard Assets, look in
    \Assets\Standard Assets\Characters\FirstPersonCharacter\Scripts
    The MouseLook.cs script has the code you need for rotating the camera.

    If you still want to do it yourself you need to learn about how to use Quaternions.
    https://docs.unity3d.com/ScriptReference/Quaternion.html
     
  4. Jofnir_IceSesh

    Jofnir_IceSesh

    Joined:
    Jan 27, 2017
    Posts:
    72
    Because I dont like the idea of using someone elses code. Yes convenient, but if I use assets how will I ever learn for myself
     
  5. dragonice501

    dragonice501

    Joined:
    Dec 2, 2015
    Posts:
    146
    Code (CSharp):
    1. void UpdateRotation ()
    2.     {
    3.         horizontal = Input.GetAxis ("Mouse X") * mouseSensitivity;
    4.         vertical -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
    5.         vertical = Mathf.Clamp (vertical, -80, 80);
    6.  
    7.         controller.transform.Rotate (0, horizontal, 0);
    8.         Camera.main.transform.localRotation = Quaternion.Euler(vertical, 0, 0);
    9.     }
     
  6. benjaminben

    benjaminben

    Joined:
    Feb 2, 2016
    Posts:
    1
    Sure, it's great to get hands on and obtain a deep understanding of how things work at their core. But my guess is you'll ultimately want to stand on the shoulders of giants. Programmers everywhere, from Google to...really anywhere, regularly use and contribute to open source libraries rather than "reinventing the wheel". Because ultimately the most important thing is not that your fps controls were hand coded by you, but that your game works reliably and is fun to play!
     
    vertig02 likes this.
  7. Alex-Slavski

    Alex-Slavski

    Joined:
    Dec 29, 2017
    Posts:
    4
    While it makes sense when you just start learning unity if you actually want to be making games with it you are making yourself a horrible disservice if you are not using assets
     
  8. gamedeveloper0

    gamedeveloper0

    Joined:
    Jun 10, 2015
    Posts:
    40
    Well the truth is to use assets only where they are necessary.
     
  9. Officer_Gadget

    Officer_Gadget

    Joined:
    Aug 25, 2018
    Posts:
    1
    Hi guys,
    I need a little help here. I downloaded a first person controller package and now i can only look left and right.
    I have this.
    Code (CSharp):
    1. void UpdateRotation ()
    2.    
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class PlayerLook : MonoBehaviour
    8. {
    9.     public Transform playerBody;
    10.     public float mouseSensitivity;
    11.  
    12.     float xAxisClamp = 1.0f;
    13.  
    14.     void Awake()
    15.     {
    16.         Cursor.lockState = CursorLockMode.Locked;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         RotateCamera();      
    22.     }
    23.  
    24.     void RotateCamera()
    25.     {
    26.         float mouseX = Input.GetAxis("Mouse X");
    27.         float mouseY = Input.GetAxis("Mouse Y");
    28.  
    29.         float rotAmountX = mouseX * mouseSensitivity;
    30.         float rotAmountY = mouseY * mouseSensitivity;
    31.  
    32.         xAxisClamp -= rotAmountY;
    33.  
    34.         Vector3 targetRotCam = transform.rotation.eulerAngles;
    35.         Vector3 targetRotBody = playerBody.rotation.eulerAngles;
    36.  
    37.         targetRotCam.x -= rotAmountY;
    38.         targetRotCam.z = 0;
    39.         targetRotBody.y += rotAmountX;
    40.  
    41.         if(xAxisClamp > 90)
    42.         {
    43.             xAxisClamp = 90;
    44.             targetRotCam.x = 90;
    45.         }
    46.         else if(xAxisClamp < -90)
    47.         {
    48.             xAxisClamp = -90;
    49.             targetRotCam.x = 270;
    50.         }
    51.  
    52.         print(mouseY);
    53.  
    54.  
    55.         transform.rotation = Quaternion.Euler(targetRotCam);
    56.         playerBody.rotation = Quaternion.Euler(targetRotBody);
    57.  
    58.  
    59.     }
    60.  
    61.  
    62.  
    63. }
    64.  
    65.  
     
  10. imdashraful17

    imdashraful17

    Joined:
    Jan 7, 2019
    Posts:
    4
    You should use that component and none will be able to detect that. Or if you like re inventing wheel leave Unity and try DirectX with C++ or VB
     
  11. monsoul7

    monsoul7

    Joined:
    Mar 12, 2020
    Posts:
    1
    I'm re-inventing the wheel here because Unity 2019's Standard Asset First Person Controller is broken. Better to have something that you know works than something that's going to break and you can't figure out how to fix.
     
Thread Status:
Not open for further replies.