[Unity3D Getting Started Tutorial] using C # script to control the game objects

Foreword

Using C # script to control the game objects is an essential basic skills. Unity3D script can be used are C # and javascript and so on. We speak mainly focus on C #. This article will explain how to use scripts to control the game objects in the scene.

 

1 Create a script

Click Project view Create - C # Script. Then the script named motion.cs. Double-click the script, use the editor to open it. My editor has been changed to VS2013, software, the default editor is MonoDevelop. The default follows.

using UnityEngine;
using System.Collections;

public class motion : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

First, using the keyword defines the assembly UnityEngine and two System.Collections used by default. Then the class name and file name that we define consistent. There are two classes default function Start (), and Update (). Start () executed when the script runs began, and Update () is executed in every frame of the game to run.

 

2 motion control block

We create a Cube, and then use a script to write their own control its movements. If scenarios already have a Cube, in order to make our C # script to run with the start of the game, we need to bind Hierarchy script of an object on. We wish to put the script on the box it binding. Binding script is very simple manner, with the mouse to drag the script Scene Hierarchy view or view corresponding to the object. Alternatively, you can select an object, then drag the script to the margin of the object Inspector view.

In the script Start () function to add this sentence:

Debug.Log("hello unity");

Then click the Run button game, you can see the following information in the console window indicates that the script ran successfully.


Next, we use the rotary control script box, panning, zooming.

(1) rotation

this.transform.Rotate(Vector3.up*1,Space.World);

The above statement is written in the Update function, each frame represents 1 degree of rotation along the Y-direction in the world coordinate system. If you want along their own coordinate system, then Space.World changed Space.Self.

this.transform.Rotate(Vector3.up*1,Space.Self);


(2) translation

this.transform.Translate(Vector3.up*0.1f,Space.World);

The above statement is written in the Update function, it represents 0.1m each frame moves in the Y direction in the world coordinate system. Unity default length in meters. If you want along their own coordinate system, then Space.World changed Space.Self.

this.transform.Translate(Vector3.up*0.1f,Space.Self);


(3) Zoom

this.transform.localScale = new Vector3(1 +Mathf.Sin(Time.time), 1 + Mathf.Sin(Time.time), 1 + Mathf.Sin(Time.time));

The above statement is written to the Update function, the statement can make time to do the zoom box with a sinusoidal law of motion.

 

3 controls a plurality of objects

In front of us when the control object, use the "this", it refers to an object that the script is bound. If we want to control multiple objects, you need to use public variable bindings dynamically with a script object or objects to find.

Create a Cube in the three scenarios, namely Cube1, Cube2, Cube3. The script written binding to the camera.


(1) Use public variables

The contents of the script set as follows.

using UnityEngine;
using System.Collections;

public class motion : MonoBehaviour {

	public GameObject cube1;
    public GameObject cube2;
    public GameObject cube3;

	void Start () {
        Debug.Log("hello unity");
	}
	
	// Update is called once per frame
	void Update () {
        cube1.transform.Translate(Vector3.up*0.1f, Space.World);
        cube2.transform.Rotate(Vector3.up*1,Space.Self);
        cube3.transform.localScale = new Vector3(1 + Mathf.Sin(Time.time), 1 + Mathf.Sin(Time.time), 1 + Mathf.Sin(Time.time));
	}
}


Cube drag corresponding to the script variables.



(2) using a script dynamic binding

If you use a script to automatically bound object, the following script is used.

using UnityEngine;
using System.Collections;

public class motion : MonoBehaviour
{

    GameObject cube1;
    GameObject cube2;
    GameObject cube3;

    void Start()
    {
        cube1 = GameObject.Find("Cube1");
        cube2 = GameObject.Find("Cube2");
        cube3 = GameObject.Find("Cube3");
    }

    // Update is called once per frame
    void Update()
    {
        cube1.transform.Translate(Vector3.up * 0.1f, Space.World);
        cube2.transform.Rotate(Vector3.up * 1, Space.Self);
        cube3.transform.localScale = new Vector3(1 + Mathf.Sin(Time.time), 1 + Mathf.Sin(Time.time), 1 + Mathf.Sin(Time.time));
    }
}

(3) operation results as shown



Published 65 original articles · won praise 265 · views 550 000 +

Guess you like

Origin blog.csdn.net/zzlyw/article/details/54175881