- Home /
Moving the camera according to two parameters
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:
Answer by emc233 · Dec 29, 2013 at 07:57 PM
To look at the ball all you need is transform.lookAt(ball) this would be in camera code.
I had already investigated Transform.LookAt(). $$anonymous$$y problem was that it actually controls both the rotation and position of my camera, and that I myself wanted to control the rotation, so that the camera plane is constantly parallel to the gameboard plane (which tilts in every direction, like I said in the question...)
Any other idea, that would not entail using LookAt(), but that would kind of achieve the same thing (well, not exactly, but you get what I mean...)?
O$$anonymous$$, but rotation is exactly the parameter I DON'T need controlled, because I'm alrady controlling it according to the gameboard (my box) rotation. Therefore the question, I guess, is: how can I constrain the camera to rotate according to the box but in a perfect circle around the ball... ?
I reread this over and over again. Either I am not understanding you or you are trying to do something that is impossible.
Here is my last stab at the problem:
$$anonymous$$ake the camera a child of the ball and fix it a set height over it
this problem sounds pretty mathy but is now much more clear. It looks like you may have to solve a problem with vectors in 3d space and stuff to get the ball exactly in the center at all times.
In terms of unity code, I would still parent the camera to the ball object and then rotate the parent object based on a function that involves the normal of the board.
If that doesn't work, you can look at other games similar to this and observe the camera's behavior. The way that you want it seems like the ball isn't moving, but the walls are tilting and moving.
I am done with this question. Good luck in unity!
Answer by Ari-Bouaniche · Dec 30, 2013 at 12:56 AM
I'll take the liberty of posting one last picture (only two per question are allowed) to show what I get with the board tilted to the right... Still not good:
So, essentially, you don't want the player to see the rotation of this board happening, ins$$anonymous$$d you're using the rotation of the board to control the ball. Is this correct?
Your answer
![](https://koobas.hobune.stream/wayback/20220613125113im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Roll a ball camera problem? 1 Answer
Third person follow camera on a sphere 1 Answer
Multiple Cars not working 1 Answer
How to Change rotation while preserving local horizontal rotation 1 Answer