Search Unity

Rotate camera to look at two objects

Discussion in 'Scripting' started by Ganket, Dec 28, 2019.

  1. Ganket

    Ganket

    Joined:
    Apr 23, 2019
    Posts:
    2
    Hello,

    I've been playing around with unity just a little bit now and have come across a problem I'm unsure how to solve. Apologies if this is a topic found somewhere on the web, i'm not quite sure of the mathematical terminology to explain what I want to do.

    In a nutshell, I'd like to have my camera looking at and following a ball object. Currently I'm doing this in the following in a camera script:
    Code (CSharp):
    1. Vector3 desiredPosition = ball.position + offset;
    2. Vector3 smoothPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
    3.  
    4. transform.position = smoothPosition;
    5.  
    6. transform.LookAt(smoothPosition);
    This is great, however the camera needs to orbit the ball until it's also looking at the destination (hole of the golf course)

    Attached is a diagram what I'm looking for:
    - In white is what is currently happening
    - In red is the desired functionality.

     

    Attached Files:

  2. doarp

    doarp

    Joined:
    Sep 24, 2019
    Posts:
    147
    Get to know Cinemachine package.
    It has a lot going in it, but if you invest 1-2 hours reading the docs and watching a couple of tutorials you will save yourself dozens of hours going forward.
     
    Kurt-Dekker, Ganket and Madgvox like this.
  3. sssetz

    sssetz

    Joined:
    Apr 22, 2014
    Posts:
    13
    @Ganket did you ever figure this out?? currently in the same boat.
     
  4. Ganket

    Ganket

    Joined:
    Apr 23, 2019
    Posts:
    2
    Sort of, I just used Cinemachine. Its got exactly what I was looking for
     
  5. OpenSignal

    OpenSignal

    Joined:
    Jun 7, 2018
    Posts:
    1
    Hi @Ganket, which particular Cinemachine feature did you use for the golf example? I'm needing this exact solution... Thanks!