# Unity camera follow along cylinder looking for C# Expert!,Unity camera follow along cylinder Looking For C# Expert!

I have a mini project where a ball goes around a cylinder while moving forward on the cylinder. I want my camera to follow the ball and rotate towards the ball while in cameras point of view cylinder is always forward. So for example if the ball goes to the right i want the camera to follow the ball to the right but point at the cylinder all the time and not rotate with ball to the right same problem like here: Camera follow ball along cylinder - Questions & Answers - Unity Discussions

I have a code but doesn’t work the way I want.

``````Transform target;
public Transform pos;
public float speed = 10f;
void Start(){
target = GameObject.FindGameObjectWithTag ("Player").transform;
}
void LateUpdate(){

Vector3 rot = Quaternion.LookRotation (target.position - transform.position).eulerAngles;
rot.z = 0;
transform.rotation = Quaternion.Euler (rot);

}
void FixedUpdate(){
transform.position = pos.position;
}
``````

It’s difficult to understand exactly what you are going for without further description/images etc. Also the answer would need to know how you are handling the ball movement, but here is a basic camera script that follows the ball around the cylinder assuming the cylinder is at the origin;

``````public class CylinderCam : MonoBehaviour
{
//The ball transform
public Transform target;
//Camera height
public float height = 2f;
//Camera distance
public float distance = 1f;

private void LateUpdate ()
{
//The forward direction of the cylinder (replace this with your context)
Vector3 cylinderDir = Vector3.forward;
//This is the direction from the cylinder to the ball (which is just the ball's position, assuming the cylinder is at the origin
Vector3 targetDir = target.position;
//Here we use the vector projection property of the dot product to figure out the up direction of the cylinder relative to the ball
Vector3 upDir = (targetDir - cylinderDir * Vector3.Dot (targetDir, cylinderDir)).normalized;

//Move the camera to the required height and distance from the ball
transform.position = target.position + upDir * height - cylinderDir * distance;

//Here, we just figure out the look direction of the camera to the target, but instead we use our calculated up vector to rotate the camera around the cylinder
Quaternion rot = Quaternion.LookRotation (target.position - transform.position, upDir);
transform.rotation = rot;
}
}
``````