Rotating a cube on local axis c#
hi all!
Stupid question i guess... How can i get my C# script to rotate a cube 360 degrees smoothly?
I can get it working using this:
transform.Rotate(0, Time.deltaTime * speed, 0, Space.World);
But i need it to rotate locally as it has a parent. When i try the following i get errors :(
transform.localRotation = Quaternion.Euler(0,Time.deltaTime * speed,40);
Any ideas?
Answer by Garazbolg · Oct 15, 2015 at 10:27 AM
To solve your problem, why don't you simply do
transform.Rotate(0, Time.deltaTime * speed, 0, Space.Self);
Its your first code but locally.
Answer by LoneMonkey · Oct 15, 2015 at 02:38 AM
I think it needs to be something like this...
Quaternion myRotation = transform.localRotation;
transform.localRotation = new Quaternion(myRotation.x, myRotation.y + speed + Time.deltaTime, myRotation.z, myRotation.w);
transform.Rotate adds a rotation while transform.localRotation sets the rotation of the object to a specific point so by setting it to its current rotated state plus the speed on the axis you want to rotate should give the correct response
thanks buddy, it kinda works, cept i cant seem to get it spinning on only one axis, it seems to be doing it on all of them?
not sure....did see one problem with my code sorry its been a long night......speed + Time.deltaTime should be changed to speed * Time.deltaTime
Hi again,
Still having troubles :( i thought the code was working but then i realised i had an old script on it. Ive started a fresh scene and added it and the cube just stays put and doesn't move.
What could i be doing wrong? :(
Be carefull there, the x,y and z fields in a Quaternion aren't Euler angles it is way more complex then that.
We can associate a quaternion with a rotation around an axis by the following expression
w = cos(angle/2)
x = sin(angle/2)*cos(βx)
y = sin(angle/2)*cos(βy)
z = sin(angle/2)*cos(βz)
cos(βx), cos(βy) and cos(βz) are the "direction cosines" locating the axis of rotation (Euler's Theorem).
As a basic rule : DON'T touch the Quaternions fields.
To solve your problem, why don't you simply do
transform.Rotate(0, Time.deltaTime * speed, 0, Space.Self);
Its your first code but locally.
Answer by sandeepsmartest · Oct 15, 2015 at 08:38 AM
RotatingObject.transform.localEulerAngles= Vector3.Lerp(RotatingObject.transform.localEulerAngles,new Vector3(90,0,0),0.05f);
Here target rotation angle is in x axis i.e, (90,0,0) Hope this may help you Nsks
Answer by FlashX · Oct 15, 2015 at 10:24 AM
OMG You guys are amazing! Thank you all so much! I ended up going with @Garazbolg 's method.
Many kisses to all of you!! :P
I made a answer for my method so you can choose it as answer. And you're welcome.
Your answer
Follow this Question
Related Questions
vector3 rotatetowards don't work 1 Answer
How rotate diagonally a cube 2 Answers
Roatate object to player on tag 0 Answers
How to Rotate on Y Axis by 90 degrees left or right? 1 Answer
it is possible to rotate a texture ? 0 Answers