[C#] Camera RotateAround Just Rotating

Hi

I was expanding on the Roll-A-Ball Unity tutorial, and I decided that I wanted to make the camera rotate around the player when they move, so that the camera is always facing in the direction of the player. When I went to use RotateAround, instead of rotating around the player’s axis, the camera just rotated on it’s own axis, as if I was just using Rotate. Here’s my code:

    using UnityEngine;
    using System.Collections;
    
    public class _Camera : MonoBehaviour {
    
    public GameObject player;
    
    private Vector3 offset;
    // Use this for initialization
    void Start ()
    {
        offset = transform.position - player.transform.position;
    }

     // Update is called once per frame
     void Update ()
     {
	    transform.position = player.transform.position + offset;
	    transform.RotateAround (player.transform.position, Vector3.up, Input.GetAxis ("Horizontal"));
     }
     }

Many issues:

  • as the player transform evolves your offset must be set in the Update function.

  • If you want to have the camera facing the player you should use lookat()

    public class _Camera : MonoBehaviour
    {
    public GameObject player;
    private Vector3 offset;
    private Vector3 goal_pos;

      void Update ()
       {
             goal_pos = player.transform.position + Vector3.up * 15; // above player position;
             offset = goal_pos - transform.position; // in this order or it doesn't make sense
             transform.position = transform.position + offset * Time.deltaTime; // smoothing effect
              transform.lookat(player.transform.position);
      }
    

    }

If you want a more specific way, like noo smoothing, erase the Time.deltaTime

@EpiFouloux The only problem with LookAt is that the camera always rests on an overhead view of th player when you aren’t giving any inputs.