lecture 10 Maths
lecture 10 Maths
http://docs.unity3d.com/ScriptReference/Vector2.h
tml
2D Vectors & Basic Geometry
In Unity, you can use the normal operators for adding or subtracting to vectors,
and also for multiplying or dividing a vector by a number:
1 Vector2 a =new Vector2 (1 ,2);
2 Vector2 b =new Vector2 (3 ,4);
3 int val = 2;
4
5 Ve ct o r2 sum = a + b ;//or a-b
6 Vector2 mult = a * val;//or a/val
A unit vector is any vector with a length of 1; normally unit vectors are used
simply to indicate direction. A vector of arbitrary length can be divided by
its length to create a unit vector. This is known as normalizing a vector. A
vector can be normalized in Unity by calling the function public void Normalize
.1 Vector2 a =new Vector2 (1 ,2);
2 a. Normalize ();
3 Debug . Log (a. magnitude );//=1
Addition & Subtraction
Example:
Vector3 spawnPosition = enemy.position + new Vector3(1, 0, 1);
Angle
Use Case: Calculating the angle between two directions, helpful for
angle-based behaviors like aiming, targeting, or triggering events
based on angle thresholds.
Example:
float angle = Vector3.Angle(transform.forward, (target.position -
transform.position).normalized);
if (angle < maxViewAngle) {
// Target is within viewing angle
}
Magnitude
Use Case: Measuring the length of a vector, such as a distance between two
points for range detection.
Example:
float distanceToTarget = (target.position - transform.position).magnitude;
if (distanceToTarget < attackRange) {
AttackTarget();
}
Normalize
Example:
Vector3 direction = (target.position - transform.position).normalized;
transform.position += direction * speed * Time.deltaTime;
Distance
Example:
float distance = Vector3.Distance(player.position, enemy.position);
if (distance < detectionRange)
{
AlertEnemy();
}
Operations on Vectors (4/5)
The Dot Product operation (sometimes called the inner product, or, since its
result is a scalar, the scalar product) is denoted as a · b = |a| x | b| x cos (θ),
where |a| and |b| are the magnitudes of a and b, and θ is the angle in radians
between these two vectors.
For normalized vectors, the dot product is 1 if they point in exactly the same
direction; -1 if they point in completely opposite directions; and a number in
between for other cases (e.g. Dot returns zero if vectors are perpendicular).
This is a useful operation that allows to determine the angle between two
vectors. This is very useful to determine the amount of rotation needed to
face certain position, angle of views, etc.
Dot product
Dot: Measures the angle between two vectors. If it’s close to 1, the
vectors point in the same direction; if -1, they are opposite.
float dotProduct = Vector3.Dot(vectorA, vectorB);
Use Case: Checking if two objects are facing the same direction (e.g.,
checking field of view for AI).
Example:
float dotProduct = Vector3.Dot(transform.forward, (target.position -
transform.position).normalized);
if (dotProduct > 0.5f) {
Debug.Log("Target is in front");
Operations on Vectors (5/5)
In Unity, you can use Vector2.Dot to calculate the dot product, calculate the
angle with Mathf.Acos(), and multiply the radians by Mathf.Rad2Deg to obtain the
angle in degrees.
A 3D vector is a geometric object that has magnitude (or length) and direc-
tion.
http://docs.unity3d.com/ScriptReference/Vector3.h
tml
Operations with 3D vectors (1/4)
In Unity, you can use the normal operators for adding or subtracting to vectors,
and also for multiplying or dividing a vector by a number:
1 Vector3 a =new Vector3 (1 ,2 , -1);
2 Vector3 b =new Vector3 (3 ,4 ,0);
3 intval = 2;
4
5 Ve ct o r3 sum = a + b ;//ora-b
6 Vector3 mult = a * val;//ora/val
A unit vector is any vector with a length of 1; normally unit vectors are used
simply to indicate direction. A vector of arbitrary length can be divided by
its length to create a unit vector. This is known as normalizing a vector. A
vector can be normalized in Unity by calling the function public void Normalize
.1 Vector3 a =new Vector3 (1 ,2 , -7);
2 a. Normalize ();
3 Debug . Log (a. magnitude );//=1
Operations with 3D vectors (2/4)
Addition: Negation:
Example:
Vector3 up = Vector3.Cross(transform.right, transform.forward);
Operations with 3D vectors (3/4)
The Cross Product operation (also called vector product) is a binary operation
on two vectors in three-dimensional space, denoted as a x b = |a| |b| sin(θ)n.
The cross product a x b of the vectors a and b is a vector that is perpendicular
to both and therefore normal to the plane containing them. If the vectors have
the same direction or one has zero length, then their cross product is zero. You
can determine the direction of the result vector using the “right hand rule”.
Cross product can also be used to determine the normal of a plane (i.e. a
triangle in a surface), in order to calculate directions for collisions and lighting.
Lerp
This is most commonly used to find a point some fraction of the way along
a line between two endpoints (eg, to move an object gradually between those
points). This fraction is clamped to the range [0...1]. When t = 0 returns
from. When t = 1 returns to. When t = 0.5 returns the point midway between
from and to.
1 Transform target ;
2 Vector3 offset = transform . position - target . position ;
3 Vector3 targetCamPos = target . position + offset ;
4 floatsmoothing = 5f;
5
6 tr an s fo rm . p o si ti on = Ve c to r3 . Ler p ( tr a ns fo rm . po sitio n ,
7 ta rg e tC a mP o s , s m oo t hi ng * T ime .
de lt a Ti me ) ;
(There are equivalent functions in Mathf, Vector2, Color and
Material)
Lerp (Linear Interpolation)
Clamp Magnitude
Limits the length of a vector to a maximum value.
Vector3 clampedVector = Vector3.ClampMagnitude(vector,
maxLength);