Search Unity

About third person shooter

Discussion in 'Game Design' started by denemelikmis, Apr 13, 2021.

  1. denemelikmis

    denemelikmis

    Joined:
    Apr 13, 2021
    Posts:
    1
    i want to make a rpg game. for this, it needs third person camera. i want to make third person camera. how could i make this? i wonder math of the behind of the third person camera. Could someone explain it? sorry for my bad english
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    For a simple third-person camera, you don't need any math. Just parent the camera within the player object in the scene hierarchy, positioned above and behind that character.
     
    neoshaman likes this.
  3. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Unfortunately, these days the standard for third person games are orbiting camera setups, and they arent simple by any means. They tend not to work with parenting very well, as it introduces dependency loop and leads to stutter.

    For OP, I can advice to study camera code in this free controller: https://assetstore.unity.com/packages/templates/systems/3rd-person-controller-fly-mode-28647 This what helped me to understand orbit cameras. Be warned though, the character movement code in that asset is a mess, at least it was back in 2015 when I studied it.

    I'll try to describe the orbit camera code in short, assuming you know what Vector3s and Quaternions do:
    1.Define a Vector3 to serve as camera offset from the player object. Could be as simple as declaring a public Vector3 variable and filling it with something like X = 0, Y = 2, Z = 5. Or subtract your player position from the camera position and store it.
    2. Calculate the rotation of the camera. Declare 2 float variables for storing mouse movement. On Update, record changes of mouse position to those variables
    Code (CSharp):
    1.         X = X + Input.GetAxis("Mouse X")*SpeedX;
    2.         Y = Mathf.Clamp(Y + Input.GetAxis("Mouse Y")*SpeedY,LowerLimit,UpperLimit);
    3.         //Speed values are multipliers that increase rotation speed. By default, Input.GetAxis(mouse) outputs very small increments
    4.         //Mathf.Clamp is used on Y to limit vertical mouselook angles. Something like -90f to +90f
    Then use this data to get desired rotation.
    Code (CSharp):
    1.         Quaternion CameraRotation = Quaternion.AngleAxis(X,Vector3.up) * Quaternion.AngleAxis(-Y,Vector3.right);
    2.         //I found using Quaternion.AngleAxis the most stable method of rotation, which doesnt produce any flipping
    3.         //at least, when the "up" direction doesnt change during game
    4.         //Y is negative so moving mouse up raises the camera "gaze"
    X and Y are assumed to be degrees in this case. Quaternion.AngleAxis is the method that takes angle in degrees and Vector3 which is a direction to rotate around.
    3.Finally, apply the results to camera
    Code (CSharp):
    1.         camera.transform.rotation = CameraRotation;
    2.         Vector3 RotatedOffset = CameraRotation*CameraOffset;
    3.         camera.transform.position = player.transform.position + RotatedOffset;
    4.         //"camera" and "player" are corresponding GameObjects, CameraOffset is the Vector3 made in step 1
    5.         //Multiplying Quaternion by Vector3 in Unity creates a new Vector3 rotated by that Quaternion
    Take note that if orbiting camera is parented to the player, it introduces dependency loop, since the movement in such system usually performed relatively to the camera, plus the player object must rotate in the direction of movement. Do not use parenting in this case. If you need it for some reason, try to keep all the directions in "virtual" Vector3s and Quaternions before applying them to both camera and the player, and never use direct references to camera transform (such as camera.transform.forward) in the player movement code. Additionally, tweaking of script execution order may be required.
     
    Last edited: Apr 16, 2021
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,631