+ 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
2 Answers
+ 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
+ 2
Rrestoring faith thanksđ