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 T3PeS · Dec 07, 2011 at 10:58 PM · c#vector3.angle

Rotation results x value only positive.

The following class is ment to combine 2 planes into 1 arrow that is variavble in size, while the head keeps it size.

The constructor technically works, but for some reason the arrow isn´t point correctly in x direction, which means negative x values are returned as positive. I have been looking at this code for hours now, and might be just missing something:

 public class arrowGraphics

{

 GameObject pointer;
 GameObject body;

 public Vector3 From;
 public Vector3 To;
 float length;
 // Use this for initialization
 public arrowGraphics (Vector3 f, Vector3 t)
 {
     From = f;
     To = t;
     
     pointer = (GameObject)Object.Instantiate (Resources.Load ("arrowPointer"));
     body = (GameObject)Object.Instantiate (Resources.Load ("arrowBody"));
     length = Vector3.Distance (From, To);
     
     
     Vector3 tempPos0 = new Vector3 (0, (float)0.1, (float)-length);
     float scaleOnBodyZ = (float)(length - 2.5) / 5; 

scales the body to the distance between from and to

     Vector3 tempPos1 = new Vector3 (0, (float)0.1, (float)-(2.5 * scaleOnBodyZ));
     float rot = Vector3.Angle ((To - From), new Vector3 (0, 0, -length));

is ment to determine the angle between the direction of the arrow and the direction of the mesh arrow

     pointer.transform.position = tempPos0;
     if (length > 2.5) {
         body.transform.position = tempPos1;
         body.transform.localScale = new Vector3 ((float)body.transform.localScale.x, (float)body.transform.localScale.y, (float)body.transform.localScale.z * scaleOnBodyZ);
     } else {
         body.transform.position = new Vector3(0, -10, 0);
     }

puzzles the two mehses together

     pointer.transform.RotateAround (Vector3.zero, Vector3.up, rot);
     body.transform.RotateAround (Vector3.zero, Vector3.up, rot);

then rotates around 0,0,0 by the calculated degress

     pointer.transform.position = pointer.transform.position + From;
     body.transform.position = body.transform.position + From;
 }

moves the rotated meshs into place.

whole thing works for the most part, except for the x not to negative, even thought the size of x is correct.

Some explaination: The whole rotation is doen by moving the thing to 0,0,0 to perform a save rotation/scaling and then move it with the right angles to where it belongs.

For someone might wonder, if the difference between from and to is shorter than the head of the arrow, the body is moved no nomansland. could be destroyed as well, but I´m not sure how to deal with changing existing arrows yet, so I kept it. However, the should be somewher in the first half of the constructor.

any help would be apreciated.

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

Answer by Owen-Reynolds · Dec 08, 2011 at 02:23 PM

Vector3.Angle is just a dot product, and doesn't give a direction: 30 degrees clock and counter clockwise (or up, in 3D space) all give 30. Could throw in a handWritten Left/Right check off your forward Vector (if your starting lines are always up, just check x>0.)

I find quaternions easier when you want real angles. Could compute your angle as a Quat:

 Quaternion angle=Quaternion.FromToRotation(Vector3.forward*-length, To - From);

You'd then apply it with: transform.rotation = angle*transform.rotation (or else rot*angle -- the order matters.)

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 T3PeS · Dec 08, 2011 at 04:41 PM 0
Share

Thx for the answer, figured out the same this morning. Been using this in the xz plane for to long I guess. $$anonymous$$ind of thought it was just returning the counterclockwise angle between the vectors. can just x check to - from. should do it.

thx anyways

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Flip over an object (smooth transition) 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Forward angles 2 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