Camera follow and orbiting

Hi,
I want to use a RollerBall prefab, but I want to change its control
I want to make the ball move only forward and backward, so I changed the BallUserControl script to make it work. “A” and “D” used to rotate camera around the ball.
I have implemented a CameraFollow by this code:

public GameObject player;
public Vector3 offset;

void Start()
    {
        offset = transform.position - player.transform.position;
    }

void LateUpdate()
    {
        transform.position = player.transform.position + offset;
        transform.LookAt(player.transform);
    }

Next, I implement a Camera orbiting around the ball, by this code:

if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * rotateSpeed);
            transform.LookAt(player.transform);
        }
      
if (Input.GetKey(KeyCode.A))
        {           
            transform.Translate(Vector3.left * rotateSpeed);
            transform.LookAt(player.transform);
        }

Both codes work, but only separately, together they block each other
Camera Follow works fine, but orbiting doesn’t
How can I fix it?

Here’s whole code:

using UnityEngine;
using System.Collections;

public class FollowTrackingCamera : MonoBehaviour
{

    public GameObject player;
    public Vector3 offset;
    public float rotateSpeed;


    void Start()
    {
        offset = transform.position - player.transform.position;
    }

    void CameraFollow()
    {
        transform.position = player.transform.position + offset;
        transform.LookAt(player.transform);
    }

    void LateUpdate()
    {                

        CameraFollow();
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * rotateSpeed);
            transform.LookAt(player.transform);
        }
      
        if (Input.GetKey(KeyCode.A))
        {           
            transform.Translate(Vector3.left * rotateSpeed);
            transform.LookAt(player.transform);
        }
    }
}

You need to variables, a distance, and an angle, the distance will be the distamce between the camera and the player. If you press a, add to the angle, if you press d, remove from it, then set the offset x to distance * mathf.cos(angle), and the z to distance * mathf.sin(angle), then set the position to offset + playerpos

Ok, I try this, but it still doesn’t work properly
What I did wrong?

void Start()
    {
        offset = transform.position - player.transform.position;
    }

    void CameraFollow()
    {
        transform.position = player.transform.position + offset;
        transform.LookAt(player.transform);
    }

    void LateUpdate()
    {
        CameraFollow();
        if (Input.GetKey(KeyCode.D))
        {
            angle -= 0.1f;
            offset.x = offset.x * Mathf.Cos(angle);
            offset.z = offset.z * Mathf.Sin(angle);
            transform.position = offset + player.transform.position;
        }
    
        if (Input.GetKey(KeyCode.A))
        {
            angle += 0.1f;
            offset.x = offset.x * Mathf.Cos(angle);
            offset.z = offset.z * Mathf.Sin(angle);
            transform.position = offset + player.transform.position;
        }
     
    }

Don’t do offset.x = offset.x *… Use a distance instead, a distance from the camera and the player, so offset.x = dist *…
Because you’re working in c, you can’t set these values separately, do offset = new vector3(offset.x here, offset.y, offset.z here).
Remove the transform.position sets from lateupdate, and move followplayer(); line to the end of lateupdate

Hm… ok, it looks better, but still doesn’t work :smile:

void Start()
    {
        offset = transform.position - player.transform.position;
        dist = transform.position - player.transform.position;
    }

void LateUpdate()
    {
       
        if (Input.GetKey(KeyCode.D))
        {         
            angle -= 0.1f;         
            offset = new Vector3(dist.x * Mathf.Cos(angle), offset.y, dist.z * Mathf.Sin(angle));          
        }
      
        if (Input.GetKey(KeyCode.A))
        {          
            angle += 0.1f;          
            offset = new Vector3(dist.x * Mathf.Cos(angle), offset.y, dist.z * Mathf.Sin(angle));      
        }
        CameraFollow();
    }

The distance shouldn’t be a vector 3,it should be a float.

Thank You very much!
It works! Thanks :slight_smile:

Wait, wait wait
I noticed a one more little problem with this:
I start the scene and when a first press “A” or “D” the camera is going to left side of the ball, later its work properly.
What’s the reason of this?
The code again:

 void Start()
    {       
        offset = transform.position - player.transform.position;
        dist = Vector3.Distance(transform.position, player.transform.position);
    }

    void LateUpdate()
    {
       
        if (Input.GetKey(KeyCode.D))
        {                          
            angle -= 0.02f * Time.deltaTime*100;    
            offset = new Vector3(dist * Mathf.Cos(angle), offset.y, dist * Mathf.Sin(angle));          
        }
      
        if (Input.GetKey(KeyCode.A))
        {          
            angle += 0.02f * Time.deltaTime*100;
            offset = new Vector3(dist * Mathf.Cos(angle), offset.y, dist * Mathf.Sin(angle));      
        }
        CameraFollow();
    }