Search Unity

Target on

Discussion in 'Cinemachine' started by TheNekoda, Dec 16, 2021.

  1. TheNekoda

    TheNekoda

    Joined:
    Aug 8, 2021
    Posts:
    24
    Hello!

    After using the 3RD person unity asset a little, I wanted to know if it was possible to make a target on camera follow type of. The camera would follow the player and look at an object. Is there a way to script this (have the transform of the closest object with a certain tag) or is it a built in component?

    Thank you!
     
  2. TheNekoda

    TheNekoda

    Joined:
    Aug 8, 2021
    Posts:
    24
    I've gave it a thought, is it possible to get a transform out of a collision? Like if I were to have the camera hit a raycast, and if it hits a collider, then give back a transform. Is it do-able though?
     
  3. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Hello
    Yes, look at our DualTarget example scene. You can get our example scene from the Package Manager -> Cinemachine -> Samples -> Import.

    Yes.
    See the example: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html. You can get the collider or transform from RaycastHit.
     
  4. TheNekoda

    TheNekoda

    Joined:
    Aug 8, 2021
    Posts:
    24
    Thank you very much! I've read a little about the documentation, but I'll dive in deeper for sure :)
     
  5. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Sure, let us know if you get stuck along the way ;)
     
  6. TheNekoda

    TheNekoda

    Joined:
    Aug 8, 2021
    Posts:
    24
    Hello again! I was wondering how can I store the transform of the object the ray cast hits? Is it after the Vector3 direction?
     
  7. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Could you clarify? I don't understand.
     
  8. TheNekoda

    TheNekoda

    Joined:
    Aug 8, 2021
    Posts:
    24
    Sorry!

    My main goal is to have the cinemachine camera "Look at" with none. Whenever I press a key, I want a ray to be cast from the camera. It has to go through everything but a layer I'll call "Targetable". If it hits something with this layer, the script needs to send to the cinemachine camera the transform of the object targeted. My issue right now is how to collect those transforms data and store them in some kind of variable I can assign to the LookAt. Sorry if this doesn't make it more clear. I'll try to draw it.
     
  9. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    You can get the target from Raycast, and then assign it to the vcam's LookAt.

    Try this script:
    Code (CSharp):
    1. using Cinemachine;
    2. using Cinemachine;
    3. using UnityEngine;
    4.  
    5. public class VcamTarget : MonoBehaviour
    6. {
    7.     public CinemachineVirtualCamera vcam;
    8.     public float maxDistance;
    9.     public LayerMask layerMask;
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.Space))
    14.         {
    15.             if (Physics.Raycast(vcam.transform.position, vcam.transform.forward,
    16.                 out var hit, // this has information about what was hit
    17.                 maxDistance, layerMask))
    18.             {
    19.                 vcam.LookAt = hit.transform;
    20.             }
    21.         }
    22.     }
    23. }
    You can attach this script to any gameobject, then set the vcam, maxDistance, and layerMask in the editor inspector.
     
    Gregoryl likes this.