Daily Unity Notes

Andrei Marks · February 8, 2012

A quick and dirty way to make a camera follow a character? Make it a child object…argh.

Yesterday:

  • used enum & delegate in a practical way.
    • Besides simple values and object references, you also store method references in a variable. Such a variable is known as a delegate.
    • You define a delegate type as if you're creating a method, except there's no code body. After that, you can use this type to create a delegate variable, to which you can assign any method that matches the type. You can then treat this variable like a method. In fact, you can treat a delegate like a list and add multiple methods to it. All of them will be called when you invoke the variable.
    • Events (ah...these get called across multiple scripts):
      • For our purposes, an event is a restricted form of a delegate, forced to behave like a list. We could use a regular delegate variable instead and it would work just fine.
      • Both events and delegates allow methods to be added and removed from them, via myEvent += myMethod andmyEvent -= myMethod. A delegate also allows a direct assignment, via myDelegate = myMethod. Doing so replaces whatever other methods had been added to it before. We only want the former functionality and not the latter. By disallowing it altogether, we protect ourselves from a potentially hard to find bug caused by forgetting to write a single+ somewhere.
  • Cast by placing datatype in parentheses in front of the variable.
  • Declare multiple variables of same type by separating with commas in declaration line.

Remember def of static variables: exist independent of object instances. So available to other scripts.

What is Queue?

  • The System.Collections.Generic namespace contains the Queue class, which can be used to represent a first in, first out queue. By constantly moving the first entry in the queue to the end of it, we effectively get a rotating ring.Queue is a generic class that can deal with any one type of content. In this case, we use Queue<Transform> to declare a queue of Transform references.
    • Enqueue(): You can add to the end of the queue by using the Enqueue method.
    • Dequeue(): Taking out the first item is done with the Dequeue method.
    • Peek(): Additionally, you can get to the first item without removing it via the Peek method.

Random.Range has two uses:

  • Random.Range(f, f) returns float, inclusive at both ends.
  • Random.Range(i, i) returns integer, inclusive, exclusive.

What are physics materials?:

  • Physic materials are like regular materials, except they deal with collision instead of visual properties. When objects collide, what happens depends on whether they're made of stone, wood, ice, rubber, or some other substance. You use physic materials to simulate this behavior by configuring friction and bounciness.

Idea for color-based/music theory/runner:

  • http://www.luminous-landscape.com/tutorials/colour_theory.shtml

A little bit more in depth explanation of LateUpdate/Physics relationship, it’s not that it’s called last, it’s that it’s called with each discrete physics step!

  • The physics engine works by dividing time into little discrete steps – by default 0.02 seconds – during which it moves objects and then checks for collisions and triggers. It keeps doing that in a loop until it has caught up with real time. The FixedUpdate method works like Update, except that it's called once per physics step instead of once per frame. In other words, FixedUpdate is independent of the frame rate. So if you want your code to be independent of the frame rate, put it in this method.

AddForce’s second parameter, I haven’t really used it yet:

  • The AddForce method applies a force to a rigidbody, which might result in an acceleration, which builds up velocity, which results in movement. There are actually various ways to use this method, which you control with the second parameter. For example:
    • if you want to apply a specific acceleration, regardless of an object's mass, you can use the ForceMode.Acceleration option.
    • If you want to directly adjust the velocity, you can use ForceMode.VelocityChange.

First time using a static class in Unity. Static classes:

  • By marking a class as static you require that its contents are static as well. There can't be any nonstatic variables or methods and it cannot be used to create object instances. In other words, a static class is not a blueprint for objects.

There’s a gameObject.active bool.

Specular Shader:

  • The default specular shader works like the diffuse shader, except that it also has a specular color and a shininess value. The shader uses these to add a highlight to the visuals. We use this shader for Booster because it results in more vivid color changes while it rotates.

Twitter, Facebook