- Home /
Rotating Camera around Player at Certain Point in Map
Hello! I'm new to Unity and a pretty inexperienced coder in general. After finishing the Roll a Ball tutorials, I've been trying to take them a step further by creating a course for the ball to go through. Here's what it looks like:
What I'm hoping to do is smoothly rotate the camera 90 degrees when the ball reaches the middle section of the course, so the player can see where the ball is going (as shown in this picture).
I figured out a partial solution by using quaternions to rotate the camera around the ball when it reaches the middle section, but the camera jumps to its new position in a single frame, instead of rotating smoothly. I've been trying to figure out how to do this using coroutines, but I'm not making much progress. If anyone has any advice for how to do this, it would be greatly appreciated! I've attached my camera script below:
public GameObject player;
private Vector3 offset;
public int firstRotate; /*the z coordinate where
the camera should rotate to a position*/
public int rotateBack; /*the z coordinate where
the camera should rotate to its original position*/
private bool stopRotate = false; /*used to stop the
CameraRotate function from running continuously*/
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
transform.LookAt (player.transform);
CameraRotate ();
}
void CameraRotate ()
{
if ((rotateBack >= player.transform.position.z) &&
(player.transform.position.z >= firstRotate)) //player inside horizontal section
{
if ((stopRotate == false))
offset = Quaternion.AngleAxis (-90, Vector3.up) * offset;
stopRotate = true;
}
if ((player.transform.position.z >= rotateBack) |
(firstRotate >=player.transform.position.z)) //player outside horizontal section
{
if ((stopRotate == true))
offset = Quaternion.AngleAxis (90, Vector3.up) * offset;
stopRotate = false;
}
}