- Home /
Make Camera Angle Equal Circle Tangent
In my game I am currently working on there is several planets on which the player can land on (they have gravity that sucks players up too). Once the player has entered the gravity of the planet, I would like the camera angle to equal that of the tangent created by the circle (planet, its 2D), however I cannot get my head around how to do it and I have tried several formulae and other equations with no pervail.
How would I set the angle (z-axis off the camera) to equal the tangent?
Here is a quick diagrams to show my predicament (wanting feta angle to make camera's) and please not that the X axis is opposite to conventional means:
Thanks in advance, Lachee
Answer by menneske · Jul 16, 2013 at 08:53 AM
You want the camera to rotate so that the player on the planet looks like he is standing straight up?
Use the camera transforms LookAt, and use the direction from the planet to the player as the WorldUp parameter.
Thankyou so much! In my camera controller I found the direction, made the camera look at it with the direction as up and the 0'ed out the x,y rotations and it worked, perfectly!
I thought I was over thinking it :|
Answer by robertbu · Jul 16, 2013 at 02:06 AM
The easiest way is to use Vector3.Cross(). Unity is a 3D environment, so in order to generate the tangent, you need two pieces of information. The first is the vector between the center of the circle and the player (which you outline in your drawing). The second is the vector of the axis of the circle.
If your circle is really a sphere, there are an infinite number of tangents, so you need one other vector that is different than circle/player vector in order to get the tangent. Without more description, it is hard to advise you on this second vector.
This has unfortunately confused me more. Is it possible you can show some sample code so I can get a better hold on what you are trying to explain? How do I use Vector3.Cross to produce the angle I am looking for?
Vector3.Cross() is the way you get a tangent, but in rereading your question, I'm unclear about what angle you are trying to apply to the camera, and it is unclear that you need to calculate the tangent at all to solve your problem.
Is this object a sphere or a circle?
Does the camera look down the 'Z' world axis, or the camera look at the object from arbitrary views?
What are you trying to align with the tangent line? The transform.up of the camera? The transform.right of the camera?
With answers to these, I can write you a bit of script.
Thank you for your help, but menneske has given me the solution.