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
0
Question by Luke1 · Sep 08, 2012 at 08:24 AM · vector3anglevector3.angle

what is wrong with my Vector3.Angle() code?

when this code is run, the variable mydirection says 90 degrees, instead of the 0 degrees that it should show. Why is this? I have already spent days hitting my head against the wall, so any input would be great.


 AssemblyCSharp.locData[] myList = new AssemblyCSharp.locData[7];

 //A list of an object called locData

 // x   z   
 AssemblyCSharp.locData holla = new AssemblyCSharp.locData(7.57,-.84);
 myList[0] = holla;

 //the part below is where I think the problem is, did I misuse the Vector3.Angle() method? 

 mydirection = Vector3.Angle((new Vector3((float)myList[stop].getx(),0f,(float)myList[stop].getz())-transform.position),transform.up);
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 Piflik · Sep 08, 2012 at 09:54 AM 1
Share

The first thing that is wrong with it, is lacking format...fixed that for you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Sep 08, 2012 at 03:00 PM

Why would you expect 0?

Your vector, from which you subtract your position, is only in the x-z-plane, so it's naturally exactly 90° to the upvector which is (0, 1, 0) for a nonrotated object.

I guess direction is a float? What is it supposed to store? The direction as angle? if so you're doing it totally wrong ;) Are you sure that you actually need the angle? Usually you work with vectors all the time.

Vector3.Angle always returns a positive angle since there is no clear sign between two arbitrary vectors in 3D space. You might want to use Mathf.Atan2:

 Vector3 dir = new Vector3((float)myList[stop].getx(),0f,(float)myList[stop].getz()) - transform.position;
 
 
 float angle = Mathf.Atan2(dir.z, dir.x) * Mathf.Rad2Deg;

ps: I'm a bit confused about your custom class "locData". Besides that the type starts with a lowercase letter, it seems strange that you have to cast it to float and that you have to query x and z seperately... Any insight what this class (or struct?) is used for?

Is the containing namespace / class really called "AssemblyCSharp" ?

Comment
Add comment · Show 1 · 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 Luke1 · Sep 09, 2012 at 09:35 AM 0
Share

Thankyou for your reply, the y upvector does show the angle on the x-z plane so it shouldn't be 90%.

I also just found out there is a moveto method so yeah,i guess I am doing it wrong.

to do it I would need the angle so I go in the right direction, and I guess I should of used vector3 ins$$anonymous$$d of locdata, but that and the double ins$$anonymous$$d of float is because im a bit new to unity.

all you need to know about locdata is that it returns an x and z. The only way I could use helper classes was to use AssemblyCSharp in the namespace.

I already tried using Atan but it doesn't work as it gives you (angle%90)

thankyou Philipp jutzi for formatting it and Bunny83 for your answer.

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

10 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

Related Questions

What is the 3rd point of Vector3.Angle ? 1 Answer

how detect the angle of collision on disc object ? 1 Answer

Vector3.Angle returning wrong values for vectors with small components 2 Answers

How to calculate a Vector3 point having three given Vector3 points and an angle of 90 degrees? 1 Answer

change angle of vector3 0 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