- Home /
Camera Follow works partially, need help to finish the script
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 but not origin, the ball goes 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 tilt with ball. This script follows the ball when the ball rotates left/right around the cylinder, but doesn't follow the ball when the ball goes forward.
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 = 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;
}
}
You can try another way:
Create 'camera_handler' transform inside the cylinder at local pos. 0,0,0.
Put in the Camera transform in the camera_handler at 0,0,0.
Change the camera_handler y axis (through the script) with ball y axis and change the z axis with ball distance.
Calculate y angle between the ball and the cylinder forward.
Rotate the camera_handler y-axis to this angle.
Answer by tecses1 · Mar 05, 2020 at 12:48 AM
@inspired997 If I understand you correctly, you want the camera to follow the ball's position, yet look at the cylinder? I would recommend that the Camera isn't a child of any object. Sure, it's easy to keep the position the same as the ball when it's a child, but then it also takes the scale, rotation, etc etc. Instead, get the ball's transform and set the position of the camera to that, plus wherever you want the camera to be.
public transform ballPosition;
void Update(){
transform.position = ballPosition.position + new Vector3(0,10.0f, 0); //Set position 10 above the ball.
}
Then, with the cylinders transform, you can interpolate the rotation to the cylinders position.
public transform ballPosition;
public transform cylPosition;
void Update(){
transform.position = ballPosition.position + new Vector3(0,10.0f,0);
Quaternion targetRotation = Quaternion.LookRotation(cylPosition.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 10.0f);
}
Answer by DevGa_me · Mar 04, 2020 at 03:46 PM
Use this :
Vector3 cameraPos = new Vector3(playerPos.x + 10,playerPos.y + 10 ,playerPos.z + 10);
Camera.transform.position = cameraPos;
//use this as a reference // sustitute variables used with variables in your case
//here camera will always be 10 units away from the player
Answer by idcanu · Mar 05, 2020 at 05:21 AM
@inspired997 Put Some Reference image if the problem is still persisting so we can help you out.
Your answer
Follow this Question
Related Questions
Making camera follow upwards 3 Answers
Camera smooth follow behaviour 0 Answers
Camera View Problem 0 Answers
camera follow gameobject moving in angular path. 0 Answers
Move camera on Z axis instead of Y? 0 Answers