This is an old revision of the document!


General

In the editor scene window, to rotate about a select object hold Alt and press Middle Click.

Unity comes with a bunch of tools that you have to install from the package manager. This is confusing because they have both an asset store and a package manager. You can install Probuilder, which lets you design, prototype and play-test levels rapidly in the Unity Editor.

Unity’s Prefab system allows you to create, configure, and store a GameObject complete with all its components, property values, and child GameObjects as a reusable Asset. The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene.

When making changes to prefabs, don't forget to click the apply button on the inspector window of the top level prefab object, which can be hidden.

Unity uses 3 rotation systems. Intro to Quaternion Rotations (in Unity)

  1. Euler Angers - rotation about x, y, z axis
  2. Angle Axis - A normalized Vector3 representing the axis about which to rotate by float angle amount
  3. Quaternions - Angle Axis rotation representation that have been scaled to improve performance.

Calculations

Quaternion.FromToRotation()

This code snippet calculates the rotation required to rotate an object from a given starting direction (in this case the back of the object given Vector3.back) to a final direction (in this case direction to camera) using the Quaternion.FromToRotation() function.

// Get the direction to a camera
Vector3 directionToCamera = Camera.main.transform.position - transform.position;
 
transform.rotation = Quaternion.FromToRotation(Vector3.back, directionToCamera);

Quaternion.LookRotation()

Quaternion.LookRotation() gives us more control on our rotation than Quaternion.FromToRotation(), as it gives us a control of our final rotation when we finish the primary rotation (as in ending up looking at what you need to look at but being upside down). It takes in two rotations, a first priority look to direction, and then a second priority look direction.

// Get the direction to a camera
Vector3 directionToCamera = Camera.main.transform.position - transform.position;
 
Quaternion targetRotation = Quaternion.LookRotation(-directionToCamera, Vector3.up);
 
transform.rotation = Quaternion.FromToRotation(Vector3.back, directionToCamera);

Quaternions

Number primer: Numberphile video.

They are in the form of x, y, z, w. They are always normalized, so x² + y² + z² + w² = 0

Creating Quaternions

// This has to be normalized. We generally never use this
Quaternion rotation = new Quaternion(0,0,0,1);
 
// You usually create a rotation in euler and then convert to quaternion.
 
==== Convertions ====
<code c#>
// Convert from Quaternion to Euler:
Vector3 inEuler = quaternionRotation.eulerAngles;
 
// Convert Euler to Quaternion
Quaternion inQuaternion = Quaternion.Euler(inEuler);
 
// To Angle-Axis
Quaternion randomQuaternion = Random.rotation;
float angle;
Vector3 axis;
 
randomQuaternion.ToAngleAxis(out angle, out axis);
 
// And back
Quaternion rotation = Quaternion.AngleAxis(angle,axis);

Modify one rotation value to another rotation value.

SLERP - Spherical Linear Interpolation. Two ways of doing this, with a constant velocity or with a smooth variable velocity.

LERP

You can use both.

You can set a whole bunch of attributes for functions and member variables. For example:

[Serialized]
private float damage = 0f;
[Client]
private void Shoot()
{
    // Code that only runs on a client and not a server in multiplayer
}

Check the Unity Scripting API docs

        // Create a Ray at the center of the viewport
        // There is an optional LayerMask parameter used to simplify which objects should collide with the ray
        Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.0f));
        RaycastHit hit;
 
        // Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene.
        if (Physics.Raycast(ray, out hit, pickupRange)) {
            // A more elegant way to do this forthcoming, using ItemID + an item database
            if (hit.transform.gameObject.tag == "YourGunTag") {
                Destroy(hit.transform.gameObject);
            }
        }   

A good tutorial that covers this

Unity Helpers

Unity is filled with awesome functions that handle complex timing for you.

InvokeRepeating is one them. It lets you run a script on a timer at watever rate you like. docs.

  • unity_game_dev.1586650548.txt.gz
  • Last modified: 2020/04/12 00:15
  • by paul