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

Camera Follow Position and Rotation

Discussion in 'Scripting' started by siddharth3322, Nov 2, 2017.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
    I want to write follow camera script for player's position and rotation both. At present I can able to write script so that it can follow player's position but can't able to write proper code for its rotation.

    Code (CSharp):
    1.         void Update ()
    2.         {
    3.             if (target == null)
    4.                 return;
    5.            
    6.             Vector3 targetCamPos = target.position + offset;
    7.             transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
    8.    
    9.        
    10.             transform.LookAt (target);
    11.    
    12.     //        Vector3 targetCamRot = target.eulerAngles + offsetRot;
    13.     //        transform.eulerAngles = Vector3.Lerp (transform.eulerAngles, targetCamRot, smoothing * Time.deltaTime);
    14.         }
    I have used two approaches above but none of them worked for me.
    In LookAt, I can't able to set my desire rotation of camera.
    In EulerAngles camera get rotated its own place rather than related its target.

    If I put my camera object inside player's child then its working properly, through this way I can able to get position and rotation change according to the player. But I want to do this through code rather than placing my camera under player game object.

    Here is my game play over view:

    3rdpersoncameraview.png
     
  2. MaxGuernseyIII

    MaxGuernseyIII

    Joined:
    Aug 23, 2015
    Posts:
    315
    This seems like a better place for this conversation than on the answers board.

    What is your motivation for wanting the camera not to be a child of the player?
     
    siddharth3322 likes this.
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
    Yes sir I don't want to set camera as a child of player object.
    I want to write script for following purpose.
     
  4. iamvideep

    iamvideep

    Joined:
    Oct 27, 2017
    Posts:
    118
    But why is the question. You have various player controllers in unity. 3rd person controller is also present.
     
  5. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
    Can you refer me any link?
    Basically I was facing problem in camera rotation.

    I was tried with LookAt but in that I can't able to give my custom rotation.
    I want camera rotation also work as third person, at present position get followed but rotation don't.
     
  6. MaxGuernseyIII

    MaxGuernseyIII

    Joined:
    Aug 23, 2015
    Posts:
    315
    It would be nice if you answered the question we've asked twice.
     
  7. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,042
    Basically player get any damage or force then camera also get shaking based on this if I put camera child as player object.
     
  8. MaxGuernseyIII

    MaxGuernseyIII

    Joined:
    Aug 23, 2015
    Posts:
    315
    Ah. Okay. Now we're talking. You want to decouple the motion of the camera from motion of the player but still have one influence the other.

    I think there's a solution to this that doesn't involve the implementation toward which you were driving us.

    Create a parent object that is the thing that moves around, points, aims, gets controlled by the human player, et cetera.

    Make one of the child objects of that parent object what you now call the "player" object. Make another child object the camera object. When the shaking happens, make it happen only to the child of the parent player object which is what the player sees. She shaking one affect the master context object and, therefore, won't affect the camera object.
     
    iamvideep likes this.
  9. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Just jam your camera in its own hierarchy.

    Code (csharp):
    1.  
    2. Camera Base
    3.  |-> Camera Pivot
    4.   |-> Actual Camera
    5.  
    Set the location of the base to the player's location in the LateUpdate and set the Camera Pivot's rotation to whatever you want.
     
  10. RainerEngblom

    RainerEngblom

    Joined:
    Sep 10, 2020
    Posts:
    9
    If you have a moving target and you want to rotate the camera around it (with mouse pressed) at the same time that the camera is following the target, I have a camera script here. I was testing with a plane at y-pos=0 and a vehicle hovering above it at y-pos=1. That's why I don't allow the camera to go below y-pos=2.

    Code (CSharp):
    1. public class CameraBehaviour : MonoBehaviour
    2. {
    3.     public Transform target;
    4.     public float cameraDistance = 6f;
    5.  
    6.     Vector3 currPosition;
    7.     Vector3 offsetFromTarget;
    8.  
    9.     void FixedUpdate()
    10.     {
    11.         FollowTarget();
    12.         if (Input.GetMouseButton(0))
    13.         {
    14.             RotateMe();
    15.         }
    16.     }
    17.  
    18.     void RotateMe()
    19.     {
    20.         currPosition = transform.position;
    21.         currPosition += Input.GetAxis("Mouse X") * transform.right;
    22.         currPosition += Input.GetAxis("Mouse Y") * transform.up;
    23.         if(currPosition.y < 2)
    24.         {
    25.             currPosition.y = 2;
    26.         }
    27.         transform.position = currPosition;
    28.         transform.LookAt(target);
    29.     }
    30.  
    31.     void FollowTarget()
    32.     {
    33.         offsetFromTarget = (transform.position - target.position).normalized;
    34.         currPosition = target.position + (offsetFromTarget) * cameraDistance;
    35.         if (currPosition.y < 2)
    36.         {
    37.             currPosition.y = 2;
    38.         }
    39.         transform.position = currPosition;
    40.         transform.LookAt(target);
    41.     }
    42. }