Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by maniac222 · Jan 23, 2014 at 10:40 AM · c#anglemousepositiontrigonometry

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Jan 23, 2014 at 11:52 AM 0
Share

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.

avatar image Bunny83 · Jan 23, 2014 at 11:55 AM 0
Share

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.

avatar image fafase · Jan 23, 2014 at 11:58 AM 0
Share

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.

avatar image maniac222 · Jan 23, 2014 at 12:41 PM 0
Share

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!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Code Understanding - Trigonometry + Angles - Object rotating around player and facing mouse direction 0 Answers

Rotate angle 0 Answers

collider2D.bounds.Contains not working properly 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges