Hey. I have a character with a camera. When the camera rotates, the end of the z axis describes a certain arc. I need to find point A, which is the intersection of the z axis of the camera and the global axis up. I have no idea how to do this. Can you advise something?
Is this 3D or 2D?
When you say up do you mean world space up (aka Vector3.up), or do you mean “up” in the 2d sense which is actually Vector3.forward? In any case you probably are looking for Plane.Raycast, but you will need to define what your plane is. Here’s an example that could be attached to the camera GameObject:
// Maybe this is what you want? It's a vertical plane facing to the right.
Plane p = new Plane(Vector3.right, 0);
var ray = new Ray(transform.position, transform.forward);
if (p.Raycast(new Ray(transform.position, transform.forward), out float enter)) {
Vector3 thePoint = ray.GetPoint(enter);
}
This is the sine of the up/down angle multiplied by the “some distance” from the line.
Remember that Mathf.Sin()
accepts radians, NOT degrees.
If you are X degrees up, you must multiply by Mathf.Deg2Rad
before giving it to Mathf.Sin();
float pitchAngle = 30.0f; // example data
float someDistance = 10.0f; // example data
float heightOnWall = Mathf.Sin( pitchAngle * Mathf.Deg2Rad) * someDistance;
I meant 3d. I find the angle like this:
Vector3 projection = Vector3.ProjectOnPlane(transform.forward, Vector3.up);
float angle = Vector3.Angle(projection, transform.forward);
Then, knowing the cathet and the angle, I tried to find the hypotenuse and the second cathet . But below, they showed me a simpler example that suits me perfectly.
Thanks for your example, this is what I need.