- Home /
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);
The first thing that is wrong with it, is lacking format...fixed that for you.
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" ?
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
Follow this Question
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