• 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 NoUJoe · May 12, 2014 at 03:23 PM · vector3rotateangle

Trouble with working out angle between 2 points

I'm having trouble working out the angle shown. Basically, I need the angle to be just affected by the y axis if you can see what I mean? I also need the rotation of the non selected capsule to be taken into account. What I have so far works when (the non selected) capsule isn't rotated and its forward is (0,0,1).

Here's my line of code that works when there's no rotation.

yAngle = Vector3.Angle (transform.forward, new Vector3 ( (playerBounds[i].x - transform.position.x) * transform.forward.x, playerBounds[i].y - transform.position.y, (playerBounds[i].z - transform.position.z) * transform.forward.z ) ); ![alt text][1]

playerBounds[i] is just the position of the selected capsule, and anything else is the transform of the non selected capsule. [1]: /storage/temp/26531-unity+angle+help.jpg

unity angle help.jpg (45.9 kB)
Comment
Add comment · Show 1
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 robertbu · May 12, 2014 at 03:47 PM 0
Share

Your code and your words don't match to me, so I'm unsure of what you want. I can suggest a couple things to explore. If this is 2D (just X and Y), you can get an angle of a vector relative to Vector3.right using $$anonymous$$athf.Atan2(). Note that the parameters go into the function y,x. But given your drawing, reversing them might give you what you are looking for.

Second, Vector3.Angle is unsigned. If that works for you fine. If you are looking for a signed angle:

 function  SignedAngle(v1 : Vector3,v2 : Vector3, normal : Vector3) : float {
     var perp = Vector3.Cross(normal, v1);
     var angle = Vector3.Angle(v1, v2);
     angle *= $$anonymous$$athf.Sign(Vector3.Dot(perp, v2));
     return angle;
 }

0 Replies

· Add your reply
  • Sort: 

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

20 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 avatar image

Related Questions

Rotate an object using joystick 1 Answer

Rotate vector around vector? 2 Answers

Trouble with Vector3.Angle() and RotateAround() 1 Answer

Arbitrary rotation around a point works except when up flips? 1 Answer

Angle between player and rocket targetting 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