- Home /
How to find how much an object has rotated?
Hey, I'm trying to find the angle of an object from a certain point. I've tried a few things nothing seems to be working properly. I suck at explaining so here's a little drawing of what I mean:
Answer by whydoidoit · Jun 24, 2012 at 01:16 AM
var findAngle = false;
var findingAngle = false;
var lastPosition : Vector3;
var currentAngle : float;
function Update() {
if(!findAngle) findingAngle = false;
if(findAngle && !findingAngle) {
lastPosition = transform.forward;
findingAngle = true;
}
if(findingAngle) {
currentAngle = Mathf.Acos(Vector3.Dot(lastPosition, transform.forward));
}
}
This tells you the angle between the two vectors - but it's only good for 180 degrees and it will be positive no matter which way the rotation happens - did you want something else?
hm... there's no way to check if it goes past 180 either way is there?
@commodore: Best way to do it is to use the vector difference between the two points and Pythagorean's Theorem.
@commodore Ah damn I was afraid you were going to say that :)
Try this:
var findAngle = false;
var findingAngle = false;
var lastPosition : Vector3;
var currentAngle : float;
function Update() {
if(!findAngle) findingAngle = false;
if(findAngle && !findingAngle) {
lastPosition = transform.right;
findingAngle = true;
currentAngle = 0;
}
if(findingAngle) {
var newAngle = $$anonymous$$athf.Acos(Vector3.Dot(lastPosition, transform.forward));
currentAngle += (newAngle-90);
lastPosition = transform.right;
}
}
Answer by Mizuho · Jun 25, 2012 at 05:47 AM
#pragma strict
var findAngle = false;
var reference : Vector3;
var angle : float;
function Update () {
if(Input.GetKeyDown(KeyCode.N)) findAngle = !findAngle;
if(findAngle) {
reference = new Vector3(1, 0, 0); // Direction 0 degrees is in (currently positive x axis)
angle = Vector3.Angle(reference, transform.position);
if(transform.position.z - reference.y < 0) {
angle = 360 - angle;
}
}
}
Here's the updated function. It uses the positive x axis as 0 and counts degrees counter-clockwise.
Edit: Cleaned the code up.
$$anonymous$$aybe I'm just implementing your code wrong. It only gives me a value around 1.4.. Thanks for helping and sorry if I'm annoying or too much of a noob to understand
if(Input.Get$$anonymous$$ey("h"))
{
findAngle = true;
}
else
{
findAngle = false;
}
if(findAngle)
{
// Or whatever point you're basing this off...
reference = Vector3.zero;
vector = transform.position - reference;
angle = $$anonymous$$athf.Atan(vector.z / vector.x);
if(vector.z > 0) angle += 180;
}
Debug.Log("Angle since findAngle turned true: "+angle);
Gimme a second to throw this into my testing grounds then. I'll be back in about 10 $$anonymous$$utes...
Thank you! I'll take a little screencap of my problem. It's most likely a problem on my end.
I included the debugger so you could see when I'm pressing "h"
http://www.youtube.com/watch?v=0sfc$$anonymous$$IYt6j4&feature=youtu.be
That took a looong time (sorry, I became very bad at math), but I figured out a foolproof way of doing it! See my updated answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073207im_/https://answers.unity.com/themes/thub/images/avi.jpg)