• 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
0
Question by tobold · Sep 06, 2011 at 10:40 PM · angle

Angle calculations of Vectors returning distance.

I'm trying to make a re-creation of this (http://balldroppings.com/js/) simple game using Unity.

I've managed to successfully create flat surfaces (barriers) of the correct width and in the correct position between two vector points (mouse-down and mouse-up) and now i'm trying to get the correct angle.

However I try to get the angle, it always seems to be scaling from the distance between the two vectors, rather than the angle between the two vectors and I cannot understand why.

Picture to explain: http://i.imgur.com/pbJDp.png Notice the 'longer' barriers have a more inclined angle? Thats not the angles I was trying to create, it just ignores the angle between the vectors and uses the distance between the two vectors to work out the angle. How on earth is that happening?

Can anyone explain how to successfully work out the angle between two vector points and get a value between -180 and 180

 public var mousePosDown:Vector3;
 public var mousePosUp:Vector3;
 
 var barrier : Rigidbody;
 
 function OnMouseDown () 
 {
     mousePosDown2 = Input.mousePosition;
     mousePosDown = Camera.main.ScreenToWorldPoint(mousePosDown2);
 }
 
 function OnMouseUp ()
 {
     mousePosUp2 = Input.mousePosition;
     mousePosUp = Camera.main.ScreenToWorldPoint(mousePosUp2);
     
     var between:Vector3 = mousePosUp - mousePosDown;
     var distance:float = between.magnitude;
     var position:Vector3 = ((mousePosUp + mousePosDown) / 2);
     var angle = Vector3.Angle(mousePosDown, mousePosUp);
     var rotation = Quaternion.Euler(0, 0, angle);
     
     var barrierClone : Rigidbody = Instantiate(barrier, position, rotation);
     barrierClone.transform.localScale = Vector3((distance + 0.01),0.05,1);
     barrierClone.transform.position += Vector3(0,0,10);
 }
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
0

Answer by Waz · Sep 06, 2011 at 11:14 PM

Your angle calculation is wrong. You mean the angle between your prefab and the vector between the mouse points. What you have got is the angle between the vectors from the screen top-left to the two mouse points. I.e.

 Vector3.Angle(Vector3.right, between);

But that won't work either, since it is not signed. Once you understand the problem, check out Quaternion.SetFromToRotation.

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 tobold · Sep 06, 2011 at 11:28 PM 0
Share

Yeah, I see what you mean - I want this angle: http://i.imgur.com/88HCP.png (where A is mouse-down and B is mouse-up), I think I assumed that is what Vector3.Angle did. Clearly stupid of me.

So I need to calculate the vector between A and B, then calculate the angle between that vector and the X axis?

What do you mean by 'since it is not signed'?

avatar image Waz · Sep 06, 2011 at 11:33 PM 0
Share

By not signed, I meant that it never returns a negative angle, so if you tried to use it for your purpose your plane would only ever rotate counterclockwise.

You already have the vector AB, it's your "between" variable.

And it's worth repeating: don't assume what functions do, read the docs.

avatar image tobold · Sep 06, 2011 at 11:37 PM 0
Share

so does Quaternion.SetFromToRotation(mousePosDown, mousePosUp); simply return a rotation?

could I not just go;

 var rotation = Quaternion.SetFromToRotation(mousePosDown, mousePosUp);
 var barrierClone : Rigidbody = Instantiate(barrier, position, rotation);
avatar image Waz · Sep 06, 2011 at 11:50 PM 0
Share

Almost. For that syntax you need Quaternion.FromToRotation, and again, not the angle between the mouse points! Read the docs!!!!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Rotating GUI Texture By Angle 2 Answers

Random /Organic/ Weapon Inaccuracy 1 Answer

How to throw grenade in angle? 2 Answers

How to I implement a spotlight cone in vertex/fragment shader? 1 Answer

Rotating / Smooth look at direction for Player 1 Answer


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