rotate object in code unity code example

Example 1: how to change rotate with script unity

var rotationVector = transform.rotation.eulerAngles;
rotationVector.z = 0;  //this number is the degree of rotation around Z Axis
transform.rotation = Quaternion.Euler(rotationVector);
//if you put this in a coroutine and yielding for some amount of time 
//you can have something like a rotating loading icon

Example 2: how to rotate object unity

public Transform Position; // make reference in inspector

Position.Rotate(x, y, z);

Example 3: rotate player unity

//This is how you rotate your player left and rigth with the wrrow keys:

public class Test : MonoBehaviour 
{
	public float RotateSpeed = 30f;
   
    void Update () 
    {
        if (Input.GetKey(KeyCode.LeftArrow))
            transform.Rotate(-Vector3.up * RotateSpeed * Time.deltaTime);
        else if (Input.GetKey(KeyCode.RightArrow))
            transform.Rotate(Vector3.up * RotateSpeed * Time.deltaTime);
    }
}

Tags:

Css Example