C#

About

← Back

Rotate Script for AR Model

Workflow

  • This example uses an existing Unity project already set up with a static model through Vuforia.
  • Create a new C# script asset in Unity.
  • Name it Rotate.
  • Open it in MonoDevelop to edit the code as shown below.
  • Save the C# script.
  • Compile (Run) the code within MonoDevelop.
  • Drag & drop the C# script asset to the object in the Unity workspace so that it acquires the script.
  • Run the game to see the object asset rotate at the speed set by the C# script.

Code Block

using UnityEngine;
using System.Collections;

public class Rotate : MonoBehaviour {
    public int Speed;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        transform.Rotate (new Vector3 (0,Time.deltaTime*Speed,0));
    }
}

Code Block Explanation

  • The variable Speed is added so that it appears as an input in the Unity window under the object in which it is attached to.
  • The transform method is used to rotate the object.
  • Rotate uses 3 values, one for each vector direction, so the axis for rotation needs to be defined.
  • A new vector is added within Rotate.
  • The vector values are (x,y,z) and since the rotation is to be about the y-axis, the other 2 values are set to 0.
  • The Time.deltaTime method is used to define at what speed the rotation about the y-axis will occur.
  • It is multiplied by the variable Speed so that we can make the speed modification through the Unity object parameter outside of the script file.