- Home /
Rotating an object
Hello Unity Answers, I am trying to create a script to rotate my model as I turn. I got some code from Here And it works great after changing the rotation to the Y axis except the fact that it returns the rotation to 0, 0, 0. Is there a way I can edit it to where the return position is 0, 90, 0? I tried adding an if statement for turning the model then an else statement to set it to 0, 90, 0
Edit: Here is the code I have, it's essentially the same as linked above
public float smooth = 2.0F;
public float tiltAngle = 30.0F;
void Update() {
float tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
Quaternion target = Quaternion.Euler(0, TiltAroundY, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
1/ Tag your question correctly... you're on unity answers so the tag unityanswer has no meaning. You should have at least set rotate.
2/ what do you mean exactly with >> " it returns the rotation to 0, 0, 0". Edit your question with a formatted excerpt of your code where you are trying to do this.
Hi, could you edit your question and post the code for your script in the question.
Answer by chelnok · Aug 20, 2013 at 11:56 PM
Add 90 degrees to default angle: Quaternion.Euler(0, tiltAroundY +90, 0);
public float smooth = 2.0F;
public float tiltAngle = 30.0F;
void Update() {
float tiltAroundY = Input.GetAxis("Horizontal") * tiltAngle;
Quaternion target = Quaternion.Euler(0, tiltAroundY +90, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
..or make empty gameobject as parent and current gameobject as child with (0,90,0) rotation. Rotate parent.
Answer by ActionAbbas · Aug 20, 2013 at 07:38 PM
Hi, ok this is a quick answer but this is significant.
"Quaternion target = Quaternion.Euler(0, TiltAroundY, 0);"
What? I don't get what you mean, thats exactly what i have up there
hehe @ActionAbbas... even if you give a quick answer, you could have added what was wrong with that line :)
@yyamiyyugi , you have capital T to tiltAroundY in your Quaternion line, but not in your variable definition
Thats not it, in my code thats fixed, the issue is not that it isn't rotating it's that when it goes back into position it goes to (0,0,0) ins$$anonymous$$d of (0,90,0) like I need.
so if it's not that, then how about rotating the actual object 90 degrees in the editor? $$anonymous$$aybe that's it.
@Joyrider yeah I should have explained. :-)
It Was rotated to (0, 90, 0) in the editor but my code was making it rotate back to (0, 0, 0) when it was running, all i had to do was change (0, tiltAroundY, 0) to (0, tiltAroundY + 90, 0)
Answer by moghes · Aug 20, 2013 at 09:20 PM
try this,
//or use a FixedUpdate
void Update ()
{
private float horiz = Input.GetAxis("Horizontal") * tiltAngle;
transform.Rotate(new Vector3(0,horiz ,0), Space.Self);
}
All that did was make it spin rapidly, did't even unrotate its self when I was done turning like my original does
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Face Direction Movement using CharacterController 1 Answer
Distribute terrain in zones 3 Answers
problem after rotate an object 0 Answers
Why the player is not rotating and moving at the same time ? 0 Answers