Unity, C#, Tutorial 003 , Animation object position, rotation and size

August 5, 2011   ·   0 Comments

we will learn how to add a  cube object at the run time, and doing animation.

the tutorial running on web.

This Tutorial will show you how to write unity c# code to do  animation,

please step by step to follow below steps to create  your first Unity3D App.

open a Tutorial 002 project, and update the code.

update the C# Script name to “addCubeScript”.

double-click the “addCubeScript”, the editor will show out, unity default is “MonoDevelop” app to do edition.

please change the code to below.

using UnityEngine;
using System.Collections;
public class addCubeScript: MonoBehaviour {
	private GameObject cube;
	private float x=0;
    // Update is called once per frame
    void Update () {

		cube.transform.position=new Vector3 (x,2, -5);

		cube.transform.Rotate(0, 20 * Time.deltaTime, 0);

		transform.Rotate( 10 * Time.deltaTime,0, 0);


    void Start() {
            // add a new Cube object
          cube = GameObject.CreatePrimitive(PrimitiveType.Cube );
		  cube.transform.position=new Vector3 (0,2, -5);
		  cube.transform.localScale=new Vector3 (1.1f,1.2f,1.3f);


    1. Line 7:  
      void Update() is a event function, unity will call this event function every frame when the screen redraw,
      most time, this function will be call 25 time per second or more,
      unity developers use it do to data update or display,
      like us, we write the move action in this function,
      when it was be call every time, users will see the cube move.
    2. Line 9:  cube.transform.position=new Vector3 (x,2, -5);
      we change x value every time, and keep the cube every time when Update() be called.
    3. Line 13:
      Use this function to make your game frame rate independent.If you add or subtract to a value every frame chances are you should multiply with Time.deltaTime. When you multiply withTime.deltaTime you essentially express: I want to move this object 10 meters per second instead of 10 meters per frame.
    4. Line 13:
       cube.transform.Rotate(0, 20 * Time.deltaTime, 0);
      Rotate the cube 20 degree per sec.
    5. Line 15:
       transform.Rotate( 10 * Time.deltaTime,0, 0);
       for this function, we are rotate the object which we add this script, and doing rotate 10 degree per sec.
    6. Line 4~5:

      private GameObject cube;
      private float x=0;

      we make  GameObject cube private value, so the functions could access it.
      and  make x value, and save the value  each time we change  and update it.


press the "Play"  button to start the game. it will look like below picture.


Please doing  y and z position and rotation animation.

sample code:

please download from here, “Tutorial 003 , Animation  object position, rotation and  size”

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter


Readers Comments (0)

You must be logged in to post a comment.

PowenKo’s Online Store




Recent Posts