# How to find how much an object has rotated?

 0 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: car_example.jpg (45.3 kB) more ▼ asked Jun 24 '12 at 01:06 AM commodore 113 ● 8 ● 22 ● 30 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

2 answers: sort voted first
 0 `````` 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? more ▼ answered Jun 24 '12 at 01:16 AM whydoidoit 33.1k ● 12 ● 23 ● 101 This might work, let me give it a shot! Jun 24 '12 at 01:25 AM commodore hm... there's no way to check if it goes past 180 either way is there? Jun 24 '12 at 01:45 AM commodore @commodore: Best way to do it is to use the vector difference between the two points and Pythagorean's Theorem. Jun 24 '12 at 01:51 AM Mizuho @commodore Ah damn I was afraid you were going to say that :) Jun 24 '12 at 02:21 AM whydoidoit 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 = Mathf.Acos(Vector3.Dot(lastPosition, transform.forward)); currentAngle += (newAngle-90); lastPosition = transform.right; } } `````` Jun 24 '12 at 02:24 AM whydoidoit add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 ``````#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. more ▼ answered Jun 25 '12 at 05:47 AM Mizuho 392 ● 3 ● 5 ● 6 Maybe 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.GetKey("h")) { findAngle = true; } else { findAngle = false; } if(findAngle) { // Or whatever point you're basing this off... reference = Vector3.zero; vector = transform.position - reference; angle = Mathf.Atan(vector.z / vector.x); if(vector.z > 0) angle += 180; } Debug.Log("Angle since findAngle turned true: "+angle); `````` Jun 27 '12 at 12:59 AM commodore Gimme a second to throw this into my testing grounds then. I'll be back in about 10 minutes... Jun 27 '12 at 01:06 AM Mizuho Thank you! I'll take a little screencap of my problem. It's most likely a problem on my end. Jun 27 '12 at 01:08 AM commodore I included the debugger so you could see when I'm pressing "h"http://www.youtube.com/watch?v=0sfcMIYt6j4&feature=youtu.be Jun 27 '12 at 01:24 AM commodore 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. Jun 27 '12 at 01:34 AM Mizuho add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
Your answer

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5091
x288

asked: Jun 24 '12 at 01:06 AM

Seen: 406 times

Last Updated: Jul 03 '12 at 05:23 AM