Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

How to Make CAMERA FOLLOW PLAYER

Discussion in 'Scripting' started by TheSuperSp33dyS, Feb 1, 2021.

  1. TheSuperSp33dyS

    TheSuperSp33dyS

    Joined:
    Nov 25, 2020
    Posts:
    28
    Does anyone know a script for making the camera attached to the player? I dont know why I cant just put the camera as a child of the player.

    Im just trying to make a first person game, but for some reason i cant place the camera as a child of the player in order to follow player.

    Please help me.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,960
    Can you explain what is stopping you from doing that?
     
    bobisgod234 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    I don't know why you can't either.

    I also don't know any other way to do it.

    But you didn't explain to us why you can't.

    There are a lot of Unity FPS tutorials out there to check out with TONS of interesting stuff.

    Otherwise, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  4. TheSuperSp33dyS

    TheSuperSp33dyS

    Joined:
    Nov 25, 2020
    Posts:
    28
    @PraetorBlue @Kurt-Dekker
    I can't put my camera as a child of my player. (Well, technically I can but nothing will happen.)
    I suspect this is because I am using model assets that I brought onto Unity. I hate how Unity goes berserk every DAMN time I use an asset. There will almost definitely something go wrong if I use an asset.

    Whenever I put the camera as a child of the player, it follows it around. BUT, and this is a big but, IT NEVER WORKS WITH ASSETS!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Perhaps you just need an extra level of parenting.

    For instance, let's say this is your asset hierarchy

    Code (csharp):
    1. MyProblematicAsset
    2.    ItHasChildren
    3.    AndOtherChildren
    4.        WithChildren
    Odds are if you stick the camera in there, maybe something goes wrong.

    Instead, make a new prefab with the asset as part of it:

    Code (csharp):
    1. MyShinyNewRootObject
    2.    CameraOffsetter
    3.        TheMainFPSCamera
    4.    MyProblematicAsset
    5.        ItHasChildren
    6.        AndOtherChildren
    7.            WithChildren
    If there was some script on MyProblematicAsset, move it to MyShinyNewRootObject and reconnect stuff if necessary, such as animator and/or other transforms.

    Then use CameraOffsetter to align the camera with where you want.

    This sorta thing can solve so many rotational and offset issues everywhere in gamedev and Unity.
     
  6. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    I found a nice script that follows the player nicely
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you want a script, it is super simple. You'd just have a reference to a target transform (the player character, probably where they head is supposed to be), and in LateUpdate you just set the transform.position and transform.rotation the same as the target transform.

    But you should still be able to do it by childing.

    Just in here to brag?
     
  8. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    LOL Joe not at all, I battled with it a while and found this script that really worked well for me. But I do suppose that takes away a learning experience for the OP. Depends on him.
     
  9. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    I think you need to spend some time working out what is going on. Parenting the camera to the player should work ok. If it's not, try and understand why.
     
    Kurt-Dekker likes this.