How to make The Best First Person Camera in Unity

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ธ.ค. 2024

ความคิดเห็น • 69

  • @programmierenneu2241
    @programmierenneu2241 2 หลายเดือนก่อน +2

    Short and informative video. No long beating around the bush. Thank you!

  • @MichaelH-zb8gg
    @MichaelH-zb8gg 6 หลายเดือนก่อน +13

    Nice Tutorial. I've updated to 3.0.1 and it works well. The only problem with Cinemachine is setting up player rotation on camera movement. So below is a script to handle the player rotation on camera movement. Put the script below on the player.
    using UnityEngine;
    public class PlayerRotate : MonoBehaviour
    {
    private Camera mainCamera;
    private void Start()
    {
    // Find the main camera in the scene
    mainCamera = Camera.main;
    }
    private void Update()
    {
    // Rotate the player towards the camera every frame
    RotatePlayerTowardsCamera();
    }
    private void RotatePlayerTowardsCamera()
    {
    if (mainCamera != null)
    {
    Vector3 cameraForward = mainCamera.transform.forward;
    cameraForward.y = 0f; // Ignore the y-axis rotation
    if (cameraForward != Vector3.zero)
    {
    Quaternion newRotation = Quaternion.LookRotation(cameraForward);
    transform.rotation = newRotation;
    }
    }
    }
    }

    • @Hirte2000
      @Hirte2000 4 หลายเดือนก่อน +3

      if i put that script on the player, he will rotate with the camera, but the jittering is back

    • @MichaelH-zb8gg
      @MichaelH-zb8gg 4 หลายเดือนก่อน +2

      @ElekriGames strange it worked on mine but I'll check it incase I changed any other setting in cinemachine. Will look at it tomorrow.....

    • @MichaelH-zb8gg
      @MichaelH-zb8gg 4 หลายเดือนก่อน +1

      ​@ElekriGames Ok, I see what is wrong. I had two projects runing I've posted the code for use with the character controller. I'll post the code for use with rigidbody tomorrow. Sorry about that....

    • @Hirte2000
      @Hirte2000 4 หลายเดือนก่อน +1

      @@MichaelH-zb8gg if you could sent the code tomorrow it would be great im stuck like a week with this problem

    • @MichaelH-zb8gg
      @MichaelH-zb8gg 4 หลายเดือนก่อน +1

      @@Hirte2000 No prob will do...

  • @controlledsingularity8084
    @controlledsingularity8084 7 หลายเดือนก่อน +17

    Here's a short explanation of how to actually do this whitout needing cinemachine or other crap:
    Step1: create a empty game object. This is the game object you will be moving around in update and represents the target position of your camera.
    Step2: create a script that interpolates the position and rotation of your camera towards this empty game object, about 0.6-0.8 of the way per frame will do.
    Step3. Done. You are done.

    • @vinhnguyen-o5z
      @vinhnguyen-o5z 3 หลายเดือนก่อน +1

      "You are done." Why are you intimidating me

    • @tPlayerioT
      @tPlayerioT 2 หลายเดือนก่อน +2

      cinemachine is actually good. i mean, it has effects so i wont need to make them. also has other nice features but for complex system. if you only care about camera them sure dont use cinemachine because you havee to set it up. jusst as i dont like the new input system because of the set up

  • @ariatari2137
    @ariatari2137 2 หลายเดือนก่อน +3

    the short answer how to remove jittering is to replace Update() with LateUpdate() method

  • @gsus7125original
    @gsus7125original 9 วันที่ผ่านมา

    a 4th step at the end could be to toggle unity's option to make the physics step happen during the render step instead of its own step. so if you get 80fps your physics is also 80fps

  • @--l6070
    @--l6070 หลายเดือนก่อน +1

    IMPORTANT:
    I found out why you might get the jittery camera even with everything working well. The issue is when you rotate the body of the player towards the camera/orientation. I don't really know why but removing this body rotation will fix the laggy camera.

    • @dreamer_0001
      @dreamer_0001 หลายเดือนก่อน

      thanks

    • @IspectorNachet
      @IspectorNachet หลายเดือนก่อน

      However, If you need to rotate the player, and you are moving it through rigidboy, rotate it with rigidbody too.
      I was rotating it through transform and had the jitter.
      Changed to rigidbody.rotation and it worked allright

  • @problem1O1
    @problem1O1 7 หลายเดือนก่อน +1

    Cinematic cut-scene tutorial 🙌

  • @vinhnguyen-o5z
    @vinhnguyen-o5z 3 หลายเดือนก่อน

    rotating camera and other stuff is always ass for me. The inspector display one number, eulerAngle display another, which makes clamping kind of a pain

  • @Montigorable
    @Montigorable 7 หลายเดือนก่อน

    Thanks for the solution!

  • @nikz000
    @nikz000 6 หลายเดือนก่อน

    I just interpolated the rotation values between each fixedupdate in update

  • @ze1st_
    @ze1st_ 7 หลายเดือนก่อน +3

    but how did cinemachine fix this?

    • @semikoder
      @semikoder  7 หลายเดือนก่อน

      Cinemachine incorporates the solutions I've mentioned at 8:19

    • @ze1st_
      @ze1st_ 7 หลายเดือนก่อน

      @@semikoder thx. But when i enable interpolation my players movement becomes broken wtf.

  • @mikamuestudios
    @mikamuestudios 2 หลายเดือนก่อน

    This is all well and good. But I'm running into a problem. Now that I'm using Cinemachine 3.0.1, I cannot figure out how to invert the y axis of my camera movement in a way that is configurable in code.
    Setting an invert vector processor in the InputActions asset does invert the control, but I want something that can be changed in code (so the player can set the option they want).
    So I tried applying an invert vector processor override in code... But it doesn't seem to do anything (either I'm doing it wrong, or CinemachineInputAxisController is overriding it).
    There's also the "Gain" field in the CinemachineInputAxisController. And setting the Look Y (Tilt) Gain to 1 (rather than its default -1) DOES invert the y axis. But in the actual script, there's only one Gain float that applies to both the X and Y axis.. It's public, but I can't seem to access it anyway.. and the obvious: it's ONE value that's showing up as two values in the Inspector... So obviously there's stuff going on under the hood that's way beyond my understanding here (I've been back and forth with ChatGPT, which is not being super helpful cause it keeps giving me info that is depreciated/not applicable to Cinemachine 3.0.1 and can't seem to find the answer either).. I've also dug through the Cinemachine 3.0.1. API and there's nothing helpful there..
    This is absolutely INSANE to me that this is so complicated, and now I've spent HOURS trying to figure this out when it should be simple... A public, code-accessible bool for invertYAxis (and I guess one invertXAxis for the weirdos). WHAT. The. HECK!? Someone help PLEASE!

    • @rain.tamper
      @rain.tamper 13 วันที่ผ่านมา +1

      took me 3 days to figure it out, youre welcome :)

    • @rain.tamper
      @rain.tamper 13 วันที่ผ่านมา

      lol, my message was deleted, PM me if you still need help

    • @mikamuestudios
      @mikamuestudios 12 วันที่ผ่านมา

      @@rain.tamper I'm not working on this specifically anymore (it was just for a learning project), so it's not a big deal, but I am curious how you solved it, if you'd be willing to give me a brief overview. I have no idea how to send a PM on TH-cam though. Didn't even know that was a thing. :o
      Btw, even though you probably hate that you spent 3 days figuring this out, knowing it took you so long, makes me feel a lot better about myself. :p so thanks for that. At least now I know it's not some super simple thing I simply overlooked. :D

  • @salithaanildesilva1196
    @salithaanildesilva1196 5 หลายเดือนก่อน

    How do i get the first person template

  • @andrescarvajal2981
    @andrescarvajal2981 4 หลายเดือนก่อน

    i dont have event OnMouseMove :( why?

  • @sebastiangeraldohirales4935
    @sebastiangeraldohirales4935 6 หลายเดือนก่อน +1

    I can only find the cinemachine 2.9 version

    • @GamerBoi9000
      @GamerBoi9000 5 หลายเดือนก่อน

      it depends on the unity version you're using. Cinemachine 3.x is available after Unity 2023.1 (you have to use a preview version) and 2023.2 (full release)

    • @tPlayerioT
      @tPlayerioT 2 หลายเดือนก่อน

      @@GamerBoi9000 bruh

  • @JeraldTheBEAR
    @JeraldTheBEAR 4 หลายเดือนก่อน

    Oh wow. Kinda forgot about that fixedupdate kinda thing. I’m gonna go cry now

  • @funkynormalcat988
    @funkynormalcat988 หลายเดือนก่อน

    Thank you very much!

  • @qroundhawk7149
    @qroundhawk7149 3 หลายเดือนก่อน +1

    Still have jittering, followed all steps.

  • @stillzen-dev
    @stillzen-dev 4 หลายเดือนก่อน

    wish this existed 2 fookin years ago - too late to change my system now (+ i'm using version 2020) but this will save many devs going forward

  • @tPlayerioT
    @tPlayerioT 2 หลายเดือนก่อน

    i dont have these issues but cinemachine having explosion and some other stuff built in is one less work i will have to do so its great anyways

  • @StephHami
    @StephHami 7 หลายเดือนก่อน

    how do you add the Cinemachine namespace in my C# scripts

  • @Rauza31
    @Rauza31 7 หลายเดือนก่อน

    usually when utilising a camera with a rigidbody it is good to have it separated because that also prevents the camera jittering

    • @blinkachu5275
      @blinkachu5275 5 หลายเดือนก่อน +1

      It also happens on a CharacterController which isn't a Rigidbody, so that doesn't matter

    • @Rauza31
      @Rauza31 5 หลายเดือนก่อน

      @@blinkachu5275 I’ve never experienced this while using a cc before?

  • @novs735
    @novs735 2 หลายเดือนก่อน +2

    Cool video but not recommend to use motion blur in it. I want to watch those things clearly without pausing

  • @MayKayy
    @MayKayy 4 หลายเดือนก่อน

    Thank you so much you are best!!

  • @Arctic.Wolves
    @Arctic.Wolves 4 หลายเดือนก่อน

    what about godot?

  • @Th3_V3ct0rAT2
    @Th3_V3ct0rAT2 6 หลายเดือนก่อน

    Will it work on mobile devices?

  • @SirMust108
    @SirMust108 3 หลายเดือนก่อน

    hi i use your solution and my rotation problem was solved but now i cant control my character because when i want to go forward when i looking right, my character wants to go left :/ the inputs arent change here is my code
    public class PlayerController : MonoBehaviour
    {
    [SerializeField] float moveSpeed;
    [SerializeField] float sensitivity = 0.1f;
    [SerializeField] int jumpPower;
    [SerializeField] Transform camTransform;
    Transform groundCheck;
    Rigidbody rb;
    Vector3 moveDirection;
    Vector2 currentRotation;
    private void Awake()
    {
    rb = GetComponent();
    groundCheck = transform.GetChild(1);
    camTransform = transform.GetChild(0);
    }
    private void Start()
    {
    Cursor.lockState = CursorLockMode.Locked;
    }
    private void FixedUpdate()
    {
    rb.AddForce(new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed));
    }
    public void OnMove(InputAction.CallbackContext context)
    {
    Vector2 inputDirection = context.ReadValue();
    moveDirection = new Vector2(inputDirection.x, inputDirection.y);
    }
    public void OnJump(InputAction.CallbackContext context)
    {
    if (IsGrounded() && context.performed)
    {
    rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
    }
    }
    public void OnRotate(InputAction.CallbackContext context)
    {
    Vector2 mouseDelta = context.ReadValue();
    currentRotation.x += mouseDelta.x * sensitivity;
    currentRotation.y -= mouseDelta.y * sensitivity;
    currentRotation.y = Mathf.Clamp(currentRotation.y, -90f, 90f);
    }
    private bool IsGrounded()
    {
    return Physics.CheckSphere(groundCheck.position, 0.2f);
    }
    }

    • @xboxone08
      @xboxone08 2 หลายเดือนก่อน

      get a reference to the FP Camera's Transform, say cmTransform, and multiply your inputs by cmTransform.forward or cmTransform.right depending on which input axis controls which movement axis.

  • @ZeonplayzYt
    @ZeonplayzYt 7 หลายเดือนก่อน +1

    pls make tutorial on how to make cod mw type movement and camera animations

  • @StephHami
    @StephHami 7 หลายเดือนก่อน

    where do i get cm3

    • @StephHami
      @StephHami 7 หลายเดือนก่อน

      you have to have the latest version of unity

    • @sebastiangeraldohirales4935
      @sebastiangeraldohirales4935 6 หลายเดือนก่อน

      x2

    • @StephHami
      @StephHami 6 หลายเดือนก่อน

      @@sebastiangeraldohirales4935 ?

  • @Ementss
    @Ementss 4 หลายเดือนก่อน +14

    So the best way to make a first person camera is to not make one and use a plugin, great job, why make a 10 minute tutorial on failed solutions if you're just gonna use a plugin.

    • @forcepower7116
      @forcepower7116 4 หลายเดือนก่อน

      I kinda agree with you on the use of plugins, but cinemachine, the new input system and textmeshpro are all plugins that should be in Unity from the start if we are honest these 3 plugins are so powerful and Unity itself uses them

    • @YoutuberUser000
      @YoutuberUser000 หลายเดือนก่อน

      5 mins*

    • @YoutuberUser000
      @YoutuberUser000 หลายเดือนก่อน

      And the video is 9mins long not 10, that's not how you round to the nearest 😂

  • @BOLOBEEFY
    @BOLOBEEFY 2 หลายเดือนก่อน

    WOW I really needed this before but I think I already fixed the gitter but still I will switch to cinemachine , I had a really complicated problem and it randomly got fixed , I had to make the camera follow the ridgidbody and I had a ridgidbody (GUN) follow a child of the camera so it was really weird got, before that I had an even weirder problem I wanted the gun as a ridgidbody to be a child of a child of the camera that moved with the player ridgidbody obviously didnt work but I made the gun not a child then worked

  • @Goldenmonke3
    @Goldenmonke3 2 หลายเดือนก่อน

    I will switch to unreal engine

  • @srydustar5754
    @srydustar5754 6 หลายเดือนก่อน +6

    It's crazy how you have to do all those steps in Unity and in Unreal you don't 😂

    • @exhilex
      @exhilex 5 หลายเดือนก่อน

      Totally true

  • @m-is8mm
    @m-is8mm 2 หลายเดือนก่อน

    @5:22 the "step x" bit made it genuinely hard to follow. Please don't do it in future videos; every youtuber does self-deprecating humor in coding tutorials and it has never been funny and it always makes the concepts harder to follow. Make professional, to-the-point, videos; that's what we are here for. thanks. Otherwise great video.

    • @RealBrianRussell
      @RealBrianRussell 7 วันที่ผ่านมา

      I'll say I liked it. +1 and -1 makes both our comments irrelevant

  • @EuclidStreams
    @EuclidStreams 6 หลายเดือนก่อน +1

    just use late update for camera rotation lol, wtf