Comments and answers for "Rotate object in the direction where it is going"
http://answers.unity.com/questions/864640/rotate-object-in-the-direction-where-it-is-going.html
The latest comments and answers for the question "Rotate object in the direction where it is going"Comment by martin101 on martin101's answer
http://answers.unity.com/comments/866730/view.html
@Glurth thanks so much for that effort ^_^ i love Unity Answers.Thu, 01 Jan 2015 18:32:23 GMTmartin101Comment by Glurth on Glurth's answer
http://answers.unity.com/comments/866705/view.html
Good call on the Rad2Deg, totally missed that!
I also missed the divide by zero error that will occur if "h" is ZERO. To avoid the divide by zero error use Atan2(x,y), rather than the Atan(x/y) I originally posted. (http://docs.unity3d.com/ScriptReference/$$anonymous$$athf.Atan2.html)
Regarding it only making is half way through a circle: sounds like Rad2Deg, needs to be multiplied by 2.0f (though that is indeed odd, I'll test it out myself & post back what I find).
EDIT: $$anonymous$$y tests show Atan2(x,y)*Rad2Deg does indeed return a full circle, which works with AngleAxis function just fine. I used lots of Debug.Log to confirm this (as well as graphics output), to narrow down any possible issues. (though perhaps simply switching to Atan2, will do the trick (I didn't test Atan).
Here is my code, though it works on a different axis from yours it's almost identical otherwise. The last line shows how I animate the rotation to make it smoother.
Transform t = GetComponent<Transform> ();
speed.x = Input.GetAxis ("Horizontal");
speed.y = Input.GetAxis ("Vertical");
Debug.Log ("X,Y :" + speed.x.ToString() +","+speed.y.ToString());
float ang = $$anonymous$$athf.Atan2 (-speed.x, speed.y);
Debug.Log ("AngleR :" + ang.ToString());
ang = ang * $$anonymous$$athf.Rad2Deg;
Debug.Log ("AngleD :" + ang.ToString());
Quaternion qtan = Quaternion.AngleAxis (ang, new Vector3 (0, -1, 0));
t.rotation = Quaternion.RotateTowards (t.rotation, qtan, 700 * Time.deltaTime);Thu, 01 Jan 2015 17:37:07 GMTGlurthComment by Scribe
http://answers.unity.com/comments/866590/view.html
Try wrapping it in an if statement, you might have to play with the value of 0.1 that I have used as that it just a guess but it should work:
if(movement.sqr$$anonymous$$agnitude > 0.1f){
transform.rotation = Quaternion.LookRotation(Vector3.forward, new Vector3(movement.x, movement.y, 0f));
}Thu, 01 Jan 2015 13:56:55 GMTScribeComment by martin101
http://answers.unity.com/comments/866475/view.html
@Scribe
Thanks for the explanation i get it now :)
I am using unity's New 'CrossPlatFormInput' asset in which there is a Joystick provided
![alt text][1]
Everything is fine only the proble is that the Player snaps back to its original rotation ( 0 0 0 )
i guess i will have to save the last 'transform.rotation information into transform.localEulerangle (may be im wrong ) but then localEulerangle is a vector3
I even tried using
transform.localEulerAngles.x = transform.rotation.x;
transform.localEulerAngles.y = transform.rotation.y;
but this also did not work
please have a look
martin101
[1]: /storage/temp/37941-capture.pngThu, 01 Jan 2015 10:07:40 GMTmartin101Comment by martin101 on martin101's answer
http://answers.unity.com/comments/866468/view.html
Hi @Glurth
void Update()
{
float h = CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal");
float v = CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical");
float angle = $$anonymous$$athf.Atan (v / h);// * $$anonymous$$athf.Rad2Deg;
//now that we have the angle, it’s easy to generate the quaternion.
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
using this does not rotate the player and
void Update()
{
float h = CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal");
float v = CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical");
float angle = $$anonymous$$athf.Atan (v / h) * $$anonymous$$athf.Rad2Deg;
//now that we have the angle, it’s easy to generate the quaternion.
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
}
multiplying the player angle with Rad2Deg works but only for first and second quadrant i.e. upto 180 degrees
please take a lookThu, 01 Jan 2015 09:47:58 GMTmartin101Comment by Scribe
http://answers.unity.com/comments/864812/view.html
No problem! Check the [link to documentation][1] here to get started. Transform.rotation is of type 'Quaternion' which is a way of representing an orientation, what you see as rotation in the inspector is not the same, that representation is called an 'Euler angle' (and isn't so good due to ambiguity etc). So this is a Quaternion class method which produces a Quaternion type with an orientation such that the z-axis of a transform points along the first argument, and the y-axis points along the second argument given.
Hence `Quaternion.LookRotation(Vector3.forward, new Vector3(movement.x, movement .y, 0f));` creates a rotation that points your objects 'forward' direction along `Vector3.forward` and points the 'up' direction along `new Vector3(movement.x, movement .y, 0f)` which is the same as the direction of movement.
As for why it snaps back, in the code there is no refrence to clicking a button at all so I cannot help unless you also post the code concerning the button clicking/releasing!
Glad I could help,
Scribe
[1]: http://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.htmlMon, 29 Dec 2014 22:15:39 GMTScribeComment by martin101
http://answers.unity.com/comments/864804/view.html
@Scribe It worked thank you
could you please just explain me what exactly you did :)
and it snaps back to its original position after releasing the button.Mon, 29 Dec 2014 22:05:00 GMTmartin101Answer by Glurth
http://answers.unity.com/answers/864767/view.html
I think ArcTangent is the function you are looking for.
Given an x,y point on a plane, ArcTangent defines the angle between the x-axis and a line between the origin and that point as follows:
Angle = Arctan(y/x)
(http://en.wikipedia.org/wiki/Inverse_trigonometric_functions check out the section “application: finding the angle of a right triangle.”)
Use your velocity vector’s x and y in ArcTangent to get the angle.
float h = CrossPlatformInputManager.GetAxisRaw("Horizontal");
float v = CrossPlatformInputManager.GetAxisRaw("Vertical");
float angle = Mathf.Atan (v / h);
//now that we have the angle, it’s easy to generate the quaternion.
transform.rotation = Quaternion.AngleAxis (angle, new Vector3(0,0,1));Mon, 29 Dec 2014 21:28:20 GMTGlurthComment by martin101
http://answers.unity.com/comments/864762/view.html
a little help here !! :(Mon, 29 Dec 2014 20:29:20 GMTmartin101Comment by martin101
http://answers.unity.com/comments/864742/view.html
![alt text][1]
@Oliver I get the logic that i have to find the angle with calculation
but the player rotates towards X axis
[1]: /storage/temp/37792-rotation2.pngMon, 29 Dec 2014 20:03:19 GMTmartin101Comment by Scribe
http://answers.unity.com/comments/864710/view.html
perhaps the following would work:
transform.rotation = Quaternion.LookRotation(Vector3.forward, new Vector3(movement.x, movement .y, 0f));
Sorry, I hadn't read properly, this should orientate your transform so its 'forward' points along forward and its 'up' (y axis) points along the direction of 'movement'.Mon, 29 Dec 2014 19:07:28 GMTScribeComment by Oliver1135
http://answers.unity.com/comments/864692/view.html
this looks like the same problem you are having I just happened to see it, this may be the asnwer you are looking for
http://answers.unity3d.com/questions/630670/rotate-2d-sprite-towards-moving-direction.htmlMon, 29 Dec 2014 18:46:27 GMTOliver1135Comment by martin101
http://answers.unity.com/comments/864643/view.html
@2dkot please refer to this question ( i was unable to edit the previous one UA bug )Mon, 29 Dec 2014 17:08:49 GMTmartin101