title: Unity 3D - Camera follow behind main character + offset
body: <p>I use the attached c#  script to control the camera.
  <br><br>
  <strong>Mouse scroll (pulley/roller/wheel)</strong>: Zooming in and out of the main character
  <br>
  <strong>Up Arrow (or W key) and Down Arrow (or X key)</strong>: Raise and Lower the camera
  <br>
  <strong>Right arrow (or D key) and Left arrow (or A key)</strong>: Rotate the camera around the main character
  <br><br>
  I try to get the camera to follow the back of the main character, and add it the offset that player defined by using mouse and arrows.
  <br><br>
  This line correctly moves the camera according to the input from the mouse and arrows:</p>
  <pre class="lang-cs prettyprint-override"><code>transform.position = target.position + offset * currentZoom;
  </code></pre>
  <p>This line correctly moves the camera so that it will follow the back of the main character:</p>
  <pre class="lang-cs prettyprint-override"><code>transform.position = target.position - target.forward + Vector3.up;
  </code></pre>
  <p>But each of them works correctly only if the other is canceled. If I try to merge them into one line, like:</p>
  <pre class="lang-cs prettyprint-override"><code>transform.position = target.position - target.forward + Vector3.up + offset * currentZoom;
  </code></pre>
  <p>then the camera doesn't move properly:</p>
  <ol>
  <li><p>Using the left and right arrows moves the camera around the main
  character in ellipse/oval shape instead of in a circle</p>
  </li>
  <li><p>When the character moves, the offsset set by the right and left
  arrows isn't saved but the camera returns to being exactly behind
  the back of the main character</p>
  </li>
  </ol>
  <p>What do I need to do to combine the two lines so that camera does move properly?
  <br><br></p>
  <pre class="lang-cs prettyprint-override"><code>using UnityEngine;
  
  public class CameraController : MonoBehaviour
  {
      public Transform target;
      public Vector3 offset = new Vector3(10f, 6f, 0f); 
      public float RotationX = .5f;
      public float rightLeftSpeed = 5f;
  
      public float currentZoom = .13f;
      public float minZoom = .1f;
      public float maxZoom = 1f;
      public float speedZoom = .1f;
  
      public float currentHeight = 6f;
      public float minHeight = 0f;
      public float maxHeight = 10f;
      public float speedHeight = 1f;
  
      void Update()
      {
          currentZoom -= Input.GetAxis(&quot;Mouse ScrollWheel&quot;) * speedZoom;
          currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
  
          currentHeight += Input.GetAxis(&quot;Vertical&quot;) * speedHeight * Time.deltaTime;
          currentHeight = Mathf.Clamp(currentHeight, minHeight, maxHeight);
          offset.y = currentHeight;
  
          offset = Quaternion.AngleAxis(-Input.GetAxis(&quot;Horizontal&quot;) * rightLeftSpeed, Vector3.up) * offset;
      }
  
      void LateUpdate()
      {
  
          transform.position = target.position + offset * currentZoom;
          transform.position = target.position - target.forward + Vector3.up;
          transform.LookAt(target.position + Vector3.up * RotationX);
      }
  }
  </code></pre>