Wednesday 8 January 2014

Unity: Toggle GameObject active state using a shortcut key


As a daily Unity user I always found it cumbersome to press the 'set active' checkbox in the selected object's inspector view.

I wrote this originally for Unity 3.x because every time you toggled the checkbox, you would get a dialog asking you if you want to apply the change to all the children and most times you click yes.

Fortunately things changed in Unity 4.x and by default, applies to the whole hierarchy. But nevertheless, if you prefer using a shortcut over clicking that tiny checkbox, go ahead and give it shot. It's also very easy one time setup.

1. Create a folder named Editor inside Assets. It can be inside a subfolder

2. Inside this folder create an new script, replace it with this:

using UnityEditor;
using UnityEngine;
using System.Collections;

public class EditorHelper : MonoBehaviour
{
}

3. Copy paste the following into the class body

[MenuItem("GameObject/Toggle Selected GameObject _g")]
static void ToggleGameObject ()
{
  //var selected = Selection.transforms;
  if( Selection.transforms != null )
  {
    foreach( Transform t in Selection.transforms)
      t.gameObject.SetActive( !t.gameObject.activeInHierarchy );
  }
}

4. Now go to unity and select multiple objects from either the scene view or from the hierarchy. Press 'g' on the keyboard to toggle their GameObject's active state.

Wednesday 29 February 2012

Unity: Calculate Velocity of non-rigidbodies


Say you need to calculate the velocity of a gameobject in motion and you can't or dont want to put a rigidbody on it. Whats the easiest way to do it?
Here's how I calculate velocity using co-routines:


void Start ()
{
  StartCoroutine( CalcVelocity() );
}
 
IEnumerator CalcVelocity()
{
  while( Application.isPlaying )
  {
    // Position at frame start
    prevPos = transform.position;
    // Wait till it the end of the frame
    yield return new WaitForEndOfFrame();
    // Calculate velocity: Velocity = DeltaPosition / DeltaTime
    currVel = (prevPos - transform.position) / Time.deltaTime;
    Debug.Log( currVel );
  }
}

To test it out, attach it to a moving game object. Very simple and straightforward, but can be used in a lot of places.

Sunday 11 December 2011

This is the first game I've created using Adobe Flash. Scripted in Action Scrip 3.0.
Have Fun.

Friday 9 December 2011

Unity Web Player | WebPlayer
Check out my prototype/concept of a 2.5 shoot-em-up game created in Unity. I am developing further mechanics like scoring system, weapon switching, smarter enemies, 2 more types of enemies and proper levels. Will keep you updated. Meanwhile please play and comment about the game. Please don't complain about the art or design elements like player speed. Its just a demo and not a Game.Have Fun!

Contributors

Followers