Archives For Unity

Here’s how to get the rigidbody from an object the script is applied to in Unity. The example shown is in C#.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	private Rigidbody rb;

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

	// Called before any physics calculations.
	// This is where you put your physics code.
	// We apply forces to the rigid body, which is physics, so this is where it goes.
	void FixedUpdate() {
		// Pressing arrow keys on keyboard will apply force to rigidbody 
		float moveHorizontal = Input.GetAxis ("Horizontal");
		float moveVertical = Input.GetAxis ("Vertical");

		Vector3 movement = new Vector3 (moveHorizontal,0.0f,moveVertical);

		rb.AddForce (movement);
	}

}

This Unity behavior script rotates the object it’s attached to by 90 degrees when the script’s rotate function is called. Just copy and paste the contents into a new JavaScript in Unity.

#pragma strict
 
public var seconds: float = .2;
 
private var rotating = false;
 
function rotateObject (thisTransform : Transform, degrees : Vector3) {
 
    if (rotating) return;
 
    rotating = true;
 
    var startRotation : Quaternion = thisTransform.rotation;
    var endRotation : Quaternion = thisTransform.rotation * Quaternion.Euler(degrees);
    var t : float = 0.0;
    var rate : float = 1.0/seconds;
 
    while (t < 1.0) {
    	t += Time.deltaTime * rate;
    	thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
    	yield;
	}
 
	rotating = false;
 
}
 
function rotate() {
	rotateObject(transform, Vector3.forward*-90);
}

One way to call the rotate function from some other script is as follows:

 
var cube : GameObject = GameObject.Find("Cube");
cube.SendMessage("rotate");

Cheers!

Two awesome Unity behavior scripts that you can download and use for free right now!

GUIGameTimerAndGUILetterbox.zip (3 KB ZIP)

The GUI Gamer Timer runs a timer during the life of your game.  You can display the timer on the screen or simply have it running behind the scenes so that you can determine the elapsed minutes and seconds from other scripts in the game.

The GUI Letterbox script draws a cinematic letterbox on the screen which can open up to expose the full view of your game at the time you specify. Along with some good music, this can help you set a very interesting mood! The letterbox script also contains fade-in functionality which you can use alone or in combination with the letterbox.

(Click to enlarge…)

Create a new GameObject and name it ‘GameController’. Drag both scripts onto a GameObject that’s in the scene. Select the GameObject that holds the script and then look in the Inspector panel.

Continue Reading…

Learning keyboard commands in the Unity game development tool is essential to working fast and efficiently. To find them, you don’t have to search the Web for a cheat sheet; you can simply open Unity’s Preferences. From the menu bar, select Unity > Preferences…

Keyboard Commands in Unity Preferences

Once the Unity Preferences view appears, select Keys and then choose from the list of Actions. The keyboard commands for the selected action are then shown.

We recommend that you avoid changing the keyboard action settings if you’re new to Unity because the default settings may be referenced in the documentation and tutorials that you’ll be learning from.

This is the third of a multi-part series that will teach you how to build a player health status indicator for the Unity GUI. If you haven’t already, you should read Part 1 and Part 2. You might also wish to download download the starter assets, which you can then import into Unity.

Introduction

So, we’ve got our health status indicator on the screen and we can manually drag on the Health Width property in the inspector to shrink and expand the green health bar while the game is running. But, of course, we need to be able to change the health by accessing the healthWidth property from some other behavior script in the game.

Let’s imagine a scenario in which an enemy is attacking your hero. Each time the enemy gets within the vicinity of the hero or touches the hero, a script attached to the enemy game object might recognize the collision and send a signal to the health status widget to change.

In today’s post we’ll tell you how to do it and you’ll be learning a simple skill that you’ll use a lot in your Unity games – you will learn how to access global variables or properties in one behavior script from another.

Continue Reading…

Unity 4 is the most ambitious version of Unity yet and you can download the public beta now!

Following are just some of the exciting new features you can explore:

  • Mecanim, Unity’s next generation character animation engine
  • Performance Improvements
  • Collision Detection on Particles
  • Dynamic Font Rendering
  • Mobile Shadows
  • Improved Cubemap Import
  • Bumpmap Terrain
  • Project Browser
  • BakeSelected Lightmapping
  • AssetImporter.userData
  • SkinnedMeshRenderer.BakeMesh
  • AddComponent dropdown
  • SkinnedMeshRenderer Performance
  • Faster enter playmode
  • 3D (volume) texture support
  • NavMeshObstacleComponent
  • SceneView iso/perspective UI
  • Wireframe rendering
  • Improvements to Adobe Flash Export

Did you know?

  • Unity was awarded top 100 mobile company by OnMobile
  • Top 250 technology company by AlwaysOn
  • Develop Award for Best Engine
  • Over 1.2 million registered developers
  • 53% of mobile game developers report to use Unity
Unity is absolutely the best platform for young and aspiring game makers – empowering you with the ability to produce professional 2D and 3D games for multiple platforms. If you haven’t yet discovered this amazing tool, you should download the current stable release or explore the new features and updates-in-progress with Unity 4 Beta.

 

 

 

Many times, you animate your models outside of Unity and then import the animations into the game along with the models. But did you know that you can also animate directly within the Unity editor? Here is a great video from Unity Cookie that teaches you exactly how:

Greetings aspiring young game developers and welcome to the all new MasterGamecraft.com where we promise to help you master the craft of game making!

This is the second of a multi-part series that will teach you how to build a player health status indicator for the Unity GUI.

Continue Reading…

Greetings aspiring young game developers and welcome to the all new MasterGamecraft.com!

This is the first of a multi-part series that will teach you how to build a player health status indicator for the Unity GUI.

Continue Reading…