+ 3

How to rotate an object around its axis in c#?

Hi! Prompt the C# script for unity to control the object around its axis(rotation, scaling, rotation). Example of these codes: https://code.sololearn.com/WdyPr8GzyBjf/?ref=app https://code.sololearn.com/WI0qEQk2TRXe/?ref=app

13th Jan 2019, 3:15 PM
_aleksandr_g
_aleksandr_g - avatar
2 Réponses
+ 2
Each object has a transform, which contains the transformations to be applied to the object (translation, rotation, scaling, etc..). add a script (my code is in C#) to the object and in the update method apply a rotation to the transform. The rotation depends on whether or not you want the object to rotate along the x, y, or z axis. void Update(){ transform.Rotate( // Rotation amount (as a vector) ); } Example: transform.Rotate (Vector3.up * Time.deltaTime * 3); Vector3.up is a vector with the values (0, 1 , 0), so this would rotate the object along the y axis. Meaning, it will spin left/right (depending on where you look at it). Time.deltaTime gives us the amount of time elapsed in the previous frame. This will help make the rotation look 'smooth' regardless of our framerate. 3 is our speed. Try to find a nice value. See: https://docs.unity3d.com/ScriptReference/Transform.Rotate.html https://docs.unity3d.com/ScriptReference/Vector3.html https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
23rd Jan 2019, 10:00 PM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Rrestoring faith thanks👍
24th Jan 2019, 5:07 AM
_aleksandr_g
_aleksandr_g - avatar