Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

3rd Person Shooter Camera

Discussion in 'Scripting' started by JimInsane, Aug 22, 2016.

  1. JimInsane

    JimInsane

    Joined:
    Aug 22, 2016
    Posts:
    11
    Hey there!
    First post with a little problem here.

    I'm about to play a little more with Unity (I did use Unity before for little Projects and I do know how to program in C# etc.).

    This time I want to build a little 3rd Person Shooter. For this I got a few questions for the Camera:

    First I wanted to do a usual Following Camera which just follows the Rotation and Translation of the Character.
    Problem here was the UpDown Rotation.

    Then I wanted to have the Rotation Controls to control the Camera directly and the Camera controls the Characters rotation afterwards.
    As I want the character to be able to hide behind Objects, this seems pretty cool, as I just can undock the Character-Rotations from the Camera-Rotations for this moments.
    What I don't like about this is the fact, that the controls get split up into several Scripts which I do think is a bad programming-technique in regards of maintaining.

    Then I came to think about hybrid forms:
    1. I could control an Invisible Gameobject or the Gun itself which is followed by the cam and which is followed by the Character
    2. I could outsource the whole movement-stuff into the Camera and the character follows the Camera-Motion
    (if needed). But here, there are Problems with Distance-Control.
    For Example: If I want the Cam to zoom in when Running (for a nice effect) I have to do more calculation there.

    What I want to ask is:
    What would be your approach. Do I overthink this? Is there a much simpler Solution?
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,301
    Yes.

    Upon scene start store offset from camera to camera target.
    In LateUpdate, calculate new camera position by adding stored offset vector to position of the target.
    Rotation controls simply rotate stored offset vector.
    Zoom reduces/increases stored offset vector length.

    If you need to handle camera not hiding behind walls, shoot raycast + spherecast from camera target to camera, and point of contact to set camera position (do not store that point within offset vector, though).

    Basically, "offset" vector stores desired position of camera relative to the target.
    Rotate/zoom controls modify offset vectors.
    "offset" is used in LateUpdate to calculate position of the camera...
    Which is then adjusted to accomodate for collision/clipping avoidance. (adjusted position is not stored within offset vector).

    Basically, the whole thing should fit into one script.
     
  3. Billy4184

    Billy4184

    Joined:
    Jul 7, 2014
    Posts:
    5,981
    There's a great free third person controller asset on the store, that might be a good starting point for you.
     
  4. JamesArndt

    JamesArndt

    Unity Technologies

    Joined:
    Dec 1, 2009
    Posts:
    2,912
  5. JimInsane

    JimInsane

    Joined:
    Aug 22, 2016
    Posts:
    11
    Well in this Video it seems like this:

    Mouse / Right Joystick controlls camera directly (character rotates afterwards with a Lerp), while
    Direction Buttons (WASD)/ Left Joystick controls the characters movement directly.

    Another thing to note is: while aiming, the characters direction is aligned with the cameras forward while the character can independent of camera when not aiming.

    What I am going to do is this:
    I make one Script for the camera. As the camera already needs a Transform as target (in this case the Players Avatar) it has already a reference to its transform.
    This way I can do the whole controlling stuff inside the Camera Script which accesses the Character.

    In my approach the character can only move camera independend in 2 cases:
    Hiding and running.
    In all other cases (Normal walking, sneaking, aiming) the cameras forward is (about) the characters forward.

    Now Left Stick and all other buttons will control the characters movement while Right Stick will control the camera,
    sets characters align.
    This way I can make the Character do a smooth follow.

    Are there any ideas or suggestionbs on this?
     
    JamesArndt likes this.