A Script that demonstrates different ways to control & move a cube. Attach this script to a cube (or any object), and manipulate it with the WASD, N & M keys.

using UnityEngine;
using System.Collections;

public class CubeControlScript : MonoBehaviour {
	// We use this variable to keep track of our items scale
	private float itemScale = 1;

	// Update is called once per frame
	void Update () {
		// Scripts naturally have access to all of the components of the 
		// object the script is attached to. We can easily access the transform
		// of the game object to manipulate its position. Here we base the 
		// translation on the WASD keys, the rotation on the mouse, and the
		// scale on the 'N' and 'M' keys

		float xMovement = Input.GetAxis ("Horizontal") * .5f; // create a variable called xMovement and assign it to the X-axis input multipled by .5
		float yMovement = Input.GetAxis ("Vertical") * .5f; // create a variable called yMovement and assign it to the Y-axis input muultiplied by .5
		transform.Translate(xMovement, 0f, yMovement);

		float xRotation = Input.GetAxis ("Mouse Y") * 3F; // create a variable called xRotation and assign it to the Mouse Y axis multiplied by 3
		float yRotation = -Input.GetAxis ("Mouse X") * 3f; // create a variable called yRotation and assign it to the Mouse X axis mulitplied by 3
		transform.Rotate(xRotation, yRotation, 0f);

		if (Input.GetKey (KeyCode.N)) { // Sets a specific key on the keyboard for a specific function
				itemScale -= .1F; // 
		} else if (Input.GetKey (KeyCode.M)) { // sets a specific key on the keyboard for a specific function
				itemScale += .1F;
		}

		transform.localScale = new Vector3(itemScale, itemScale, itemScale); // scales the object to a specific size 
	}
}

 

Leave a Reply