- Home /
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: https://answers.unity.com/questions/1680243/camera-follow-ball-along-cylinder.html
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;
}
Answer by Namey5 · Mar 02, 2020 at 12:20 PM
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;
}
}
This scirpt works when the ball rotates around the cylinder but doesnt go forward with the ball. And the cylinder is not at origin. I took the transform of cylinder and i wrote cylinderDir = cylinder_Transform.up;
I fixed it thank you very much for the script the way i fixed it was: Vector3 v = upDir height + cylinderDir distance; transform.position = target.position + v.normalized;
Accidentally left out the target position, answer is updated.
@Namey5 Now i have a car that rotates around a cylinder. When i press left mouse click the car rotates to the left and when i press right mouse click the car rotates to the right. What i want is when i release the buttons the car should rotate to point forward along the cylinder.
rotation = Input.GetAxisRaw("Horizontal");
Vector3 yRot = Vector3.up * rotation * rotation_speed * Time.deltaTime;
Quaternion deltaRot = Quaternion.Euler(yRot);
Quaternion targetRot = rgb.rotation * deltaRot;
rgb.$$anonymous$$oveRotation(Quaternion.Slerp(rgb.rotation, targetRot, 50f * Time.fixedDeltaTime));
Your answer
![](https://koobas.hobune.stream/wayback/20220612224232im_/https://answers.unity.com/themes/thub/images/avi.jpg)