1. Vectors

What is a vector?

  • Vector is a mathematical thingy with a length and a direction
  • Often represented by an arrow (see next slide)
  • Can have multiple dimensions (also called components)
    • In our case usually 2 or 3:
    • , , and possibly
  • Video games are full of vectors!
    • Used for depicting position, velocity, acceleration, forces....
width:

2D vector example

  • This is a 2D vector

    • x-component
    • y-component
  • Vectors start from the origin, or

  • In C#:

    Vector2 vectorA = new Vector2(6,3);
    

2D vector length

  • The length of a 2D vector is given by the Pythagoras theorem

  • In C#:

    float length = Mathf.Sqrt(Mathf.Pow(vectorA.x, 2) + Mathf.Pow(vectorA.y, 2));
    
    • Vector classes have a shorthand, too: A.magnitude

Vector arithmetic

  • Manual: Understanding Vector Arithmetic
  • Let's introduce the most important vector operations
    • Addition
    • Subtraction
    • Scalar multiplication
    • Extra: vector multiplication
      • Dot product
      • Cross product (WIP)
  • C# examples included
width:

Vector addition

  • sum of two vectors is calculated by summing up the individual components



  • can be illustrated by moving to start from the endpoint of

    Vector2 A = new Vector2(3.0f, 3.0f);
    Vector2 B = new Vector2(6.0f,-2.0f);
    Vector2 C = A + B;
    
width:

Vector subtraction

  • difference of two vectors,

    • is is "flipped" :




  • starts from the endpoint of and ends in the endpoint of

    Vector2 A = new Vector2(3.0f, 3.0f);
    Vector2 B = new Vector2(6.0f,-2.0f);
    Vector2 C = A - B;
    
width:

Scalar multiplication

  • When a vector is multiplied by a scalar (a number), the vector is scaled

    • If a vector is multiplied by 2, its length doubles



    Vector2 A = new Vector2(3.0f,3.0f);
    Vector2 C = 2 * A;
    

Special cases for scalar multiplication

  • If the scalar is negative, the vector gets flipped

    • that's what happened in subtraction
  • What about division?

    • it's basically multiplication as well

Vectors in Unity

Vectors in video games

  • position
  • velocity
  • acceleration
  • rotation
  • forces

Vector classes

  • Unity has two Vector classes, Vector2 and Vector3

    • "2" and "3" here are the number of dimensions: (x,y) and (x,y,z)
    • Vector2 position = new Vector2(1.0f, 2.0f)
      
  • Vector components can be accessed with the dot notation:

    • position.x, position.y, position.z
  • Vectors can't be directly modified:

    • No: position.x = 3.0f
    • position = new Vector2(3.0f, position.y);
      
  • Length of a vector can be acquired with vector.magnitude

Velocity vector

  • Velocity is an important concept in game development!
  • It's the rate of change of position
    • new_position = old_position + velocity;
  • 3-dimensional velocity vector can be added like this to GameObject's position in an Update() method:
    • transform.position += velocity;
    • We can use player input, collision, etc. to adjust the velocity as needed

Note: Velocity vector is usually drawn so it starts from the moving object

  • (Remember, though, that vectors do not "know" its starting positions.)

Acceleration vector

  • Another important concept is acceleration
  • It's the rate of change of a GameObject's velocity!
    • new_velocity = old_velocity + acceleration;
  • We can use acceleration to change Rigidbody's velocity
    • rb.velocity += acceleration;
  • We can't use that for GameObjects without rigidbodies, but we can store velocity in a separate variable
    velocity += acceleration;
    transform.position += velocity;
    
  • This might seem like a cheap trick, but actually allows us to create smoother motion if we use player input, collision, etc to control acceleration instead of velocity

Distance vector

  • To get the distance vector between two objects, we use vector subtraction
    • vector_B - vector_A
    • transform.position - otherGameObject.transform.position;
  • To just get the length of the distance vector, a.k.a, the distance:
    • Vector2.Distance(vector_A, vector_B)

Special vectors of Unity

  • See Static properties in Script Reference: Vector3
    • Vector3.right: the global axis
    • Vector3.up: the global axis
    • Vector3.forward: the global axis

Common vector operations

Normalizing a vector

  • When we are not interested about the length of a vector, only its direction, it helps to normalize the vector
  • Normalizing means setting the length of a vector to be
  • This is achieved by dividing the vector by its length
  • in C#, the normalized version of any vector can be easily accessed:
    Vector2 UnitVector = A.normalized;
    

Rotating a vector

  • In Unity, rotation is represented by Quaternions (fin. kvaternio, kvaterniot)
  • To rotate a vector by a given angle, you can do a Quaternion rotation operation:
    Vector3 rotatedVector = Quaternion.Euler(0, 0, 90) * originalVector;
    
    • This isn't a regular multiplication, so do note that Quaternion.Euler has to be on the left side of the vector.
  • To rotate a Quaternion:
    myQuaternion *= Quaternion.Euler(0, 0, 90);
    
  • Note: the Transform and Quaternion classes have many rotation methods available, see Transform Class: Rotation

Extra: Dot product - Vector's alignment with another vector

  • If you want to know how much two vectors point to the same direction, we can use the dot product
  • The dot product returns a number, not a vector!
  • For normalized vectors, Vector3.Dot returns
    • if they point in exactly the same direction
    • if they point in completely opposite directions
    • if the vectors are perpendicular

Dot product example

  • Check if two rigidbodies are moving to the same direction:
    float alignment = Vector3.Dot(
      _rigidBody.velocity.normalized,
      otherGameObjectRigidBody.velocity.normalized)
    

Note about distance

  • Note: when performance is important, using magnitude can be a bad idea: it includes the costly square root operation
    • if you need to only compare magnitudes, or you're squaring it right away, use .sqrMagnitude instead!

Exercise 1. I'm being avoided

Create a scene with two GameObjects, a player and a static enemy.
Calculate the distance between the player and the enemy.

If the distance is smaller than some given threshold value,

  • ⭐ Change the color of the enemy (and set the color back to default when you're no more on the range)
  • ⭐⭐ Make the enemy shoot at player!
  • ⭐⭐⭐ Move the enemy farther away from the player along the shortest possible path.

Reading & watching