Search Unity

FreeLook - Third Person Over-The-Shoulder Camera

Discussion in 'Cinemachine' started by Lekyh, Jul 14, 2018.

  1. Lekyh

    Lekyh

    Joined:
    Oct 18, 2012
    Posts:
    9
    Hi,

    I'm trying create a Third Person Over-The-Shoulder Camera with Cinemachine FreeLook Camera, but i can't create the right offset for the camera.

    I used a script that user Gregoryl posted in another Thread, but the Cinemachine Collider Extension don't work with this script.

    My idea is create a camera that stays in the right of the player and that he it always look at to the center point of the screen when the buttons are pressed.

    Please, help me.

    Current camera:
    upload_2018-7-14_12-37-9.png

    With Gregoryl's Script (the player don't look at the center of screen):
    upload_2018-7-14_12-39-8.png

    Cinemachine Freelook Camera Component:
    Lookat.png


    My script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float speed = 5.0f;
    8.     public float jumpSpeed = 5.0f;
    9.     public float gravity = -Physics.gravity.y;
    10.  
    11.     private Ray ray;
    12.     private RaycastHit hit;
    13.     private Vector3 moveDirection;
    14.     private Vector3 targetPosition;
    15.     private Quaternion targetRotation;
    16.     private CharacterController controller;
    17.     private Camera cam;
    18.  
    19.     void Start ()
    20.     {
    21.         controller = GetComponent<CharacterController>();
    22.         cam = GameObject.Find("Main Camera").GetComponent<Camera>();
    23.     }
    24.  
    25.     void Update ()
    26.     {
    27.         if (controller.isGrounded)
    28.         {
    29.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    30.             moveDirection = transform.TransformDirection(moveDirection);
    31.             moveDirection *= speed;
    32.  
    33.             if (Input.GetButton("Jump"))
    34.             {
    35.                 moveDirection.y = jumpSpeed;
    36.             }
    37.         }
    38.  
    39.         moveDirection.y -= gravity * Time.deltaTime;
    40.         controller.Move(moveDirection * Time.deltaTime);
    41.  
    42.         if(Input.GetButton("Horizontal")  || Input.GetButton("Vertical"))
    43.         {
    44.             targetPosition = cam.transform.forward * 1000;
    45.             targetPosition.y = transform.position.y;
    46.  
    47.             targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
    48.  
    49.             transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 3 * Time.deltaTime);
    50.         }
    51.     }
    52. }

    Gregoryl's Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. [ExecuteInEditMode]
    5. [SaveDuringPlay]
    6. [AddComponentMenu("")]
    7.  
    8. public class CameraOffset : CinemachineExtension
    9. {
    10.     [Tooltip("Offset the camera's position by this much (camera space)")]
    11.     public Vector3 m_Offset = Vector3.zero;
    12.  
    13.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam,
    14.         CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    15.     {
    16.         if(stage == CinemachineCore.Stage.Aim)
    17.         {
    18.             state.PositionCorrection += state.FinalOrientation * m_Offset;
    19.         }
    20.     }
    21. }
     
    Last edited: Jul 15, 2018
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I'm having a little trouble understanding exactly what you want, because "looks at the center point of the screen" is ambiguous. There is no center point of the screen, it's a line that starts at the camera and goes to infinity. At what point along that line do you wish the player to look?
     
  3. Lekyh

    Lekyh

    Joined:
    Oct 18, 2012
    Posts:
    9
    I'm getting the "center point of the screen" with code:

    Code (CSharp):
    1. targetPosition = cam.transform.forward * 1000;
    or code:

    Code (CSharp):
    1. Ray ray = cam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 1000));
    Basically I get the 1000 point point of that vector. The camera will have a offset to the right relative to the player and the player will have to look at this point.

    Sorry for my english. I'm brazilian.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Ah yes, I should have seen that from your code :)

    Two things:
    1. Small error in your script: target position should be
    Code (CSharp):
    1. targetPosition = cam.transform.position + cam.transform.forward * lookAtDistance;
    and
    2. The Offset script will work with Collider if you order the extensions correctly (just drag them in the inspector). It should be like this:
    upload_2018-7-16_12-8-32.png
     
  5. Lekyh

    Lekyh

    Joined:
    Oct 18, 2012
    Posts:
    9
    I put it exactly as you asked and it did not work:

    I'm using the Unity 2018.2.0f2 with Cinemachine 2.2.0.

    upload_2018-7-16_13-31-38.png
    upload_2018-7-16_13-32-1.png
    upload_2018-7-16_13-32-10.png

    In addition, the collision is not triggered when the player is covered, but rather when the offset point is covered.

    upload_2018-7-16_13-34-39.png
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    What do you mean "it did not work"? Looking at your images, it seems to me that the player is facing in the correct direction relative to the camera.

    The collider will displace the camera if there is an obstacle between it and the LookAt target. If you order the extensions properly, this calculation will be made after the offset is applied.

    It works for me when I try to repro based on your description. I'm not sure exactly what you're doing. Maybe you should upload a sample project that shows the problem.
     
  7. Lekyh

    Lekyh

    Joined:
    Oct 18, 2012
    Posts:
    9
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Yes, you're right, it doesn't work. My bad for not paying enough attention.
    Solution: Get rid of the Camera Offset script, and set the Screen X in all 3 FreeLook composers to 0.3 or something like that.
     
    Lekyh likes this.
  9. Lekyh

    Lekyh

    Joined:
    Oct 18, 2012
    Posts:
    9
    Yeaaaah! It work!

    Thanks for your support. You are a F***ing god!
     
  10. godratio

    godratio

    Joined:
    Nov 19, 2016
    Posts:
    3
    If that what it takes to be a god im a super god lol.