Hey everyone,
I have been trying for hours now to come up with a satisfactory method for this, and I can’t, so hopefully some charitable folk here can give it a whirl and help me out… Thank you!
Here’s the situation:
I have a (rigidbody) ball rolling in a (maze-like) box. The box’s rotation angles in X and Z are movable (I did that with Input.GetAxis) so that the box can tip backward / forward and left / right depending on what arrow key is pressed.
My problem lies with the Camera. I would LOVE IT if my camera could rotate in the same way as the box but, at the same time, I need it to follow the ball (which needs to be at the center of the screen) and to be at the exact same distance from it at all times.
I’ve managed to make the camera rotate, but it’s the part about keeping the ball at the center of the screen that is driving me completely nuts, when I’m sure it’s relatively easy I am under the impression that the camera should be able to rotate around the ball in a circle (as I want the distance between it and the ball to be constant) but I can’t, for the life of me, figure out how to do that…
I’m relatively new to coding and I’m using C#. Here’s what I was able to achieve so far:
public GameObject ball;
public GameObject box;
private Vector3 offsetPosition;
private Vector3 offsetRotation;
void Start () {
offsetPosition = transform.position - ball.transform.position;
offsetRotation = transform.eulerAngles - box.transform.eulerAngles;
}
void LateUpdate () {
transform.position = ball.transform.position + offsetPosition;
Quaternion offset;
offset = Quaternion.Euler (offsetRotation);
transform.rotation = box.transform.rotation * offset;
}
Thank you so very much for any help you might give me!
EDIT: My question seems to be unclear, so I have taken a few screenshots of my scene to illustrate my point and what I am trying to do. Here goes.
This is what I want:
And this is what I have now, with the script I have copied above.
Note: The ball gameObject in the script above is actually not the ball itself but a child Empty which is located at the exact center of the ball, and which is constrained by a small script to no rotation:
transform.eulerAngles = new Vector3 (0, 0, 0);
With the gameboard tilted to the left: