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
Please dont spamm tags. This has nothing to do with debugging, optimizations, script errors, … and you are not even looking for a C# expert either, since this is an algorithmic problem which has nothing to do with C#.
Please stop these attempts to make your thread look more important than others. People will just get annoyed and not answer instead, if anything.
As for your problem: it always helps if you try to visualize what you want to achieve, and then define incremental steps that get you there. If i understood you correctly (mostly based on the image), what you want is the camera to have some distance to the ball on the forward axis of the cylinder, as well as some distance to the cylinder itself, the direction of which depends on the rotation of the ball around the cylinder and should match it.
So here is how you can approach this problem:
Get the position of the ball ‘B’
Get the center of the cylinder below the ball ‘C’
Move the point on the backwards (-forward) axis of the cylinder by some distance, representing the offset you want the camera to be behind the ball. Let’s call this point ‘COff’
We now need a vector pointing from the center of the cylinder to the ball, which represents the direction we need to move the camera into. To get this vector, we can simply calculate ‘rotaDirection’ = B - C. We want this directional vector to be normalized, so either save the .normalized value or Normalize() the vector. So for example rotaDirection = rotaDirection.normalized.
Now we can apply this direction to our COff point, by adding rotaDirection multiplied with some distanceMultiplier(which determines the distance we want to be over the cylinder). Let’s call the resulting location ‘cameraOffset’.
This resulting cameraOffset is basically the finished product. We have calculated a point which is some defined distance behind the point and over the cylinder according the the rotation the ball has around the cylinder. This only puts the camera at the right location, so we still need to rotate it to look at the ball. Luckily there is a method called LookAt(), which does just that. If you want to apply some additional changes to your rotation (like look a bit more upwards), you need to apply them after this step.
Edit*: You may need to set the transform.up of the camera to rotaDirection before using LookAt, otherwise it may look at it in an unexpected way (ie if the ball is unter the cylinder, the camera may look up to the ball instead of down to the ball).
Hope this helps and also gives you an idea for how to approach these problems in the future