So these are classes that you write, that extend one of the Unity classes. There’s a good example on one of the Learn Unity tutorial site here. But basically  you will create the class file like this:

using UnityEngine; 
using System.Collections; 

    //It is common to create a class to contain all of your 
    //extension methods. This class must be static. 

    public static class ExtensionMethods
{
    //RULE 1 : extension methods must be declared static. 
    //RULE 2 : The first parameter must have the keyword "this"
    //RULE 3 : Follow the "this" with the Class name that you are extending, 
    // example, Vector3, Transform, Rigidbody
    //RULE 4 : Follow the class type with a variable name to use in the method
    // then just do whatever you need to in the method as per usual

    public static void ResetTransformation(this Transform trans)
    {
        trans.position = Vector3.zero;
        trans.localRotation = Quaternion.identity;
        trans.localScale = new Vector3(1, 1, 1);

    }

}

Now you can refer to your new extension method with the following code

//Notice how you pass no parameter into this
//extension method even though you had one in the
//method declaration. The transform object that
//this method is called from automatically gets
//passed in as the first parameter.
transform.ResetTransformation();

it is the same as saying

ExtensionMethods.ResetTransformation(yourTransform);

So I was watching a tutorial and the guys keeps banging on about classes being monobehaviour or not and I started wondering why he was making note of one that wasn’t monobehaviour. So I’ve looked it up on Unity and you can see the whole post here, but the main jist is this.

MonoBehaviour is the base class from which every Unity script derives.

When you use C#, you must explicitly derive from MonoBehaviour. When you use UnityScript (a type of JavaScript), you do not have to explicitly derive from MonoBehaviour.

Note: There is a checkbox for disabling MonoBehaviour on the Unity Editor; it disables Start()Awake()Update()FixedUpdate(), and OnGUI() from executing when unticked. If none of these functions are present in the script, the Editor does not display the checkbox.

Camera.orthographicSize

Did you know that the camera is half-size when in orthographic mode? Well I didn’t, but now I do.

The orthographicSize property defines the viewing volume of an  orthographic  Camera. In order to edit this size, ensure the Camera is first set to orthographic either through script or in the Inspector. The orthographicSize is half the size of the vertical viewing volume. The horizontal size of the viewing volume depends on the aspect ratio.

So if I wanted to create a bounding box around the orthographic camera view – for example in a game where I wanted to know if an object has left the gameplay area which is the whole screen – then I could use the following script.

Vector3 scaleDesiredOfColiderBox;

scaleDesiredOfColiderBox.z = 10;
scaleDesiredOfColiderBox.y = Camera.main.orthographicSize * 2;
scaleDesiredOfColiderBox.x = scaleDesiredOfColiderBox.y * Camera.main.aspect;

 

 public static void Destroy(Object obj, float t = 0.0F);

The Unity docs have a good description of the Destroy method here. But in case you just want some quick code here it is.

To destroy a gameobject from within itself, just call 

 Destroy(gameObject);

To destroy a gameobject from within after 5 seconds

 Destroy(gameObject, 5);

To destroy a script instance from a gameobject

 Destroy(this);

To destroy a component on a gameobject, in this example a rigidbody

 Destroy(GetComponent<Rigidbody>());

There is a really neat way that you can turn on and off all your assistant testing code in a file. At the top of your file put the following code

#define DEBUG_FILEASSIST

and then put all you debugging and testing code within a similar set of hash tags thus,

#if DEBUG_FILEASSIST

    private void OnDrawGizmos()
    {
        Debug.Log("Text: ");

        if (Application.isPlaying)
        {
            Gizmos.color = Color.white;
            Gizmos.DrawWireSphere(mousePoint3D, 0.2f);
            Gizmos.DrawLine(transform.position, mousePoint3D);
        }
    }
#endif

now if you comment out the  ##define DEBUG_FILEASSIST , then all code and comments between the #if DEBUG_FILEASSIST will automatically be commented out as well. You don’t need to go through the whole file to find all your debugs and helper code. It will look like this

//#define DEBUG_FILEASSIST

#if DEBUG_FILEASSIST

    private void OnDrawGizmos()
    {
        Debug.Log("Text: ");

        if (Application.isPlaying)
        {
            Gizmos.color = Color.white;
            Gizmos.DrawWireSphere(mousePoint3D, 0.2f);
            Gizmos.DrawLine(transform.position, mousePoint3D);
        }
    }
#endif

Neat eh!

Transform.LookAt

public void LookAt(Transform targetVector3 worldUp = Vector3.up);

Rotates the transform so the forward vector points at /target/’s current position.

Then it rotates the transform to point its up direction vector in the direction hinted at by the worldUp vector. If you leave out the worldUp parameter, the function will use the world y axis. 

So if I want the gameObject to look at the mouse pointer in a 2D top down game, I could use the following code to rotate the gameObject.

private Vector3 mousePoint3D; 

mousePoint3D = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.back * Camera.main.transform.position.z);

transform.LookAt(mousePoint3D, Vector3.back);

 

Transform.SetParent

If you have an app where you will need to instantiate a gameobject as a child of another gameobject, then you will need to use Transform.SetParent to do this. Unity actually does a nice document on explaining this here.  But in case you don’t want to link through, here is the main bit of the code you need.

define your child which is a game object and the transform of the desired parent gameobject clip 

public GameObject child;
public Transform parent;

Then you assign/remove the child and parent in the following ways

// Sets "newParent" as the new parent of the child GameObject.
child.transform.SetParent(newParent);

// Same as above, except worldPositionStays set to false
// makes the child keep its local orientation rather than
// its global orientation.
child.transform.SetParent(newParent, false);

// Setting the parent to ‘null’ unparents the GameObject
// and turns child into a top-level object in the hierarchy
child.transform.SetParent(null); 

 

Transform.InverseTransformPoint

Transforms position from world space to local space.

This function is essentially the opposite of Transform.TransformPoint, which is used to convert from local to world space.

Note that the returned position is affected by scale. Use Transform.InverseTransformDirection if you are dealing with direction vectors rather than positions.

This is just going to be a post about things I pick up as I study

 

So what is a vector3? In Unity it most often describes one of two things

huacanacha describes it thus : A Vector3 is a 3-tuple (an ordered list of 3 values) which can represent:

  • A vector: direction and magnitude, but no position
  • A point: position only

 

Vector3.back  is shorthand for   Vector3(0,0,-1);

Vector3.zero  is shorthand for writing  Vector3(0,0,0);

Vector3.left  is shorthand for   Vector3(-1,0,0);