- Home /
Add a Vector3 to the rotation of an obj and have it behave with logic!
Hello, I am adding vector3 to the rotation of an obj (I think .rotation is in eurler angles). And any time the y axis gets out of place I CANT rotate the obj in any logical form! For instance I'll add 90° to the x-axis with the why is pointing away from me and it will add 90 to the right axis BUT IT TURNS around some other axis...
Basically I'm wondering how to add vector3 to the rotation of the object, and get logical results (Im rotating in LOCAL space).
The process I am using: Grabbing the objects original rotation, before rotating. Creating a new vector3 by adding the original rotation to a New vector3 with the 90° increment on the respective axis. Then I am rotating object by that new vector3 I am directly changing the rotation of the object to that new vector3 over, time.time- starttime/(Number of secs)
I even know what face is facing the player, and I know where the y-axis is pointing in space. By knowing that I can choose over which axis to rotate.
Code: (I apologize for the MASS of code) NOTE: gObjectBuilder is the GameObject im trying to rotate...
if (CanRotateBuilderObj) {
gObjectBuilderOriginalRotation = gObjectBuilder.transform.rotation.eulerAngles;
}
//I have rotate back right and left as well, this is just one set (Fwd)
if (RotateFwd && CanRotateBuilderObj) {
if (gObjectBuilderBackFaceIsFacingPlayer) {
if (gObjectBuilderPositiveYAxisIsFacingUp) {
StartCoroutine(gObjectBuilderRotateCoroutine());
gObjectBuilderNewRotation = gObjectBuilderOriginalRotation + new Vector3(90f,0,0);
CanRotateBuilderObj = false;
}
if (gObjectBuilderPositiveYAxisIsFacingDown) {
StartCoroutine(gObjectBuilderRotateCoroutine());
gObjectBuilderNewRotation = gObjectBuilderOriginalRotation + new Vector3(-90f,0,0);
CanRotateBuilderObj = false;
}
if (gObjectBuilderPositiveYAxisIsFacingRight) {
StartCoroutine(gObjectBuilderRotateCoroutine());
gObjectBuilderNewRotation = gObjectBuilderOriginalRotation + new Vector3(0,-90f,0); //Double CHECK could be +90f :P
CanRotateBuilderObj = false;
}
if (gObjectBuilderPositiveYAxisIsFacingLeft) {
StartCoroutine(gObjectBuilderRotateCoroutine());
gObjectBuilderNewRotation = gObjectBuilderOriginalRotation + new Vector3(0,90f,0); //Double CHECK could be -90f :P
CanRotateBuilderObj = false;
}
}
//That coroutine that is being started
IEnumerator gObjectBuilderRotateCoroutine() {
gObjectBuilder.rigidbody.freezeRotation = false;
while ((Time.time - RotatingStartTime) <= 0.5f) {
gObjectBuilder.transform.eulerAngles = Vector3.Lerp(gObjectBuilderOriginalRotation, gObjectBuilderNewRotation, (Time.time - RotatingStartTime)/0.5f);
yield return null;
}
gObjectBuilder.rigidbody.freezeRotation = true;
CanRotateBuilderObj = true;
}
THANKS, Daniel
@aldonaletto I Noticed you helped some one else out with a similar issue, any ideas? Im wanting to stick with using vector3's not just adding a float to one of the vecotor3's axises.
Here was that someone else: http://answers.unity3d.com/questions/234007/rotate-object-over-time-90-degrees-at-a-time.html
Ive learned that you DO NOT want to be messing with quanternions so im using eurlerangles
@robertbu I noticed you help some one with this very thing here http://answers.unity3d.com/questions/462073/how-to-use-euler-angles-to-rotate-an-object-.html
Trouble is I need to read those eulerAngles to take a snapshot in time of the transforms rotation before rotating... How can I accomplish this?? I read that whole answer you gave to that guy and i noticed you said to NEVER read the eulerAngles... I dont know how to do what I want without it? any $$anonymous$$gestions? Thanks for your time!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Object wont rotate in the opposite direction 1 Answer
How is the rotation of a Transform converted into a Vector3 in the inspector ? 2 Answers
Lerping euler angles make entire spin 3 Answers
How do I make create a rotateable object that follows a raycast hit point? 2 Answers