- Home /
Trigonometry: finding the vector3 positon of one corner using one side and one angle.
Hello, I've been stuck with this problem for days and it's really killing me.
I have a perspective camera rotated 90 degrees on the x-axis that is positioned 20 units up along the y-axis.
When I click somewhere on the screen I want to instantiate an object in that direction, and I want this objects position to be 0 on the y-axis. This does not work however.
This is the code I have so far.
void Update () {
if(Input.GetMouseButton(0)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
angle = Vector3.Angle(Vector3.down, ray.direction);
hypotenuse = ray.origin.y / Mathf.Cos(angle);
pos = ray.origin + ray.direction * hypotenuse;
Instantiate(debug, pos, Quaternion.identity);
}
}
When I do this, sometimes the generated position ends up in the opposite direction and it doesn't under any circumstances have the y-axis value of 0.
Am I not using SOHCAHTOA right?
I could just have a huge, flat collider with transform.position.y = 0 and instantiate objects on the hit.point of the ray, but I don't want to do this because it's ugly and I won't learn anything if I keep running from my problems.
Any help at all would be appreciated.
Answer by Bunny83 · Jan 23, 2014 at 11:38 AM
Your problem is that Vector3.Angle returns an angle in degrees while Mathf.Cos expects an angle in **radians** like in most programming languages. You can use Mathf.Deg2Rad to convert it from degree to radians, just multiply the angle with this constant (which is PI/180 or (2*PI/360))
if(Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
angle = Vector3.Angle(Vector3.down, ray.direction) * Mathf.Deg2Rad;
hypotenuse = ray.origin.y / Mathf.Cos(angle);
pos = ray.GetPoint(hypotenuse);
Instantiate(debug, pos, Quaternion.identity);
}
However, as alternative you could use the Plane class like this:
void Update ()
{
if(Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up,Vector3.zero);
float dist;
if (groundPlane.Raycast(ray, out dist))
{
pos = ray.GetPoint(dist);
Instantiate(debug, pos, Quaternion.identity);
}
}
}
Since the mathematical plane doesn't change you can create it once in Awake and store it in a member variable.
But he does not want the plane... One thing comes to my $$anonymous$$d with your implementation, it might not be what he is after. See you are projecting the point parallel to the forward of the camera, I think he wants to extend along the direction of the ray onto the plane.
Does he say that somewhere? The plane class is just a mathematical plane. It's just pure math, nothing more. It has nothing to do with the physics system or colliders.
He said it in a previous comment to a previous answer I removed for being wrong, my bad... I was telling not to bother with calculation but in a process of learning he wants to use trig.
Thank you oh so much! I used the first solution you presented and I got the results I wanted! In my naivety I assumed that $$anonymous$$athf.Cos wanted a value in degrees and not radians.
And when I said I didn't want a plane, I meant an actual physical plane. A mathematical plane is fine, I was actually just now looking at line/plane intersections if I couldn't solve it this way.
But anyway, as far as I'm concerned, this problem is solved!