Skip to main content
Share Your Experience: Take the 2024 Developer Survey
38 votes

Benefit of using Vector2 data type over separate x & y variables

First of all, it's way less clutter. If you have a position, a velocity and an acceleration, that's already 6 variables you have to deal with, 9 in 3d. Secondly, and this is the most important part, ...
Bálint's user avatar
  • 14.9k
36 votes

If a 3D vector represents a point, how can it have a length?

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)? That's exactly that. Among other things, a vector can represent a point (a position), a direction and/or a ...
Vaillancourt's user avatar
  • 16.3k
35 votes
Accepted

How can I rotate an object based on another's offset to it?

The answer is actually pretty easy if you do the math. You have a fixed distance of Y and a variable distance of X (See Picture 1). You need to find out the angle between Z and X and turn your turret ...
John Hamilton's user avatar
28 votes
Accepted

How can I get the normal vector for a plane from a set of vertices?

You could easily find the normal by calculating two vectors, V1 = P2-P1, and V2 = P3-P1, and then find the cross product N = V1 x V2. Then you normalize N. Depending on the ordering of the vertices (...
Hermetix's user avatar
  • 507
27 votes

Why can't I use the operator '>=' with Vector3s?

Defining >= for a Vector3 type makes no sense. What determines if one vector is greater than another? Their magnitude or ...
Evgeny Vasilyev's user avatar
20 votes
Accepted

If a 3D vector represents a point, how can it have a length?

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)? The tl;dr answer may be: Yes, you can imagine it like that. But I'm not sure whether this might not lead ...
Marco13's user avatar
  • 673
16 votes
Accepted

Why can't I use the operator '>=' with Vector3s?

To simplify the answer, Vector3 is a custom struct provided by the UnityEngine namespace. ...
Gnemlock's user avatar
  • 5,263
16 votes

What are some good examples of the practical usages of cross product in game development?

Generating Normals As Maximus Minimus notes in the comments, when we have a mesh without normal vectors (say just raw vertex positions from a procedural generator or 3D scanned point cloud), we can ...
DMGregory's user avatar
  • 135k
15 votes
Accepted

How can I "zero out" velocity in an arbitrary direction?

This is pretty simple linear algebra. You can make a little helper method for it: ...
DMGregory's user avatar
  • 135k
14 votes

Benefit of using Vector2 data type over separate x & y variables

Compare the function signatures of both RotatePoints versions. Lone variables: ...
Stephane Hockenhull's user avatar
13 votes
Accepted

Moving a 2D object along circular arc between two points

You can use Bezier Curves. Given 2 given points, you have to define one third point yourself that controls where the curve is made. In your example you can create the point between your two given ...
Tom Tsagkatos's user avatar
11 votes
Accepted

Why does a unit vector show direction?

First we get a vector from B to A, as in the following picture: Now we have a vector that tells us how to get exactly from B to A. In code it looks something along these lines: ...
Lasse's user avatar
  • 3,241
11 votes

Benefit of using Vector2 data type over separate x & y variables

Readability > Writeability I feel like it's just slow me down and barely has any benefit other than organizing your code. You are correct in that it (slightly) slows you down writing that code. ...
Raphael Schmitz's user avatar
10 votes
Accepted

Unity: transform.LookAt(target) not "looking at" target?

The Problem: Your eyes default rotation makes them look to the negative X axis. However, Transform.LookAt() rotates the transform, so that the Transform.forward looks at the target. The Transform....
Chillersanim's user avatar
9 votes

What are normal, tangent and binormal vectors and how are they used?

I know this is old but i would like to add a little practical information. As stated by others but more specifically related to graphical programming. A Normal aka (a surface normal) is orthogonal to ...
WillMotil's user avatar
8 votes

How can I find the perpendicular to a 2D vector?

If , then a-perp obtained by a counterclockwise rotation by 90 degrees, i.e., See this link :)
Yas's user avatar
  • 181
8 votes

If a 3D vector represents a point, how can it have a length?

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)? You can see it that way, but only seeing it that way may lead to a wrong understanding. First of all, a ...
Polygnome's user avatar
  • 803
8 votes

Benefit of using Vector2 data type over separate x & y variables

Operator overloading. Vector v3 = v2 + v1; There is now only one place in your code where you have to write, test and debug vector addition, as opposed to tens, ...
Maximus Minimus's user avatar
7 votes
Accepted

Unwanted roll when rotating camera with pitch and yaw

As I explained above, this is not an issue with quaternions. Quaternions just represent your rotation. The problem is that you have mutually conflicting goals for this rotation: The camera can look ...
DMGregory's user avatar
  • 135k
6 votes
Accepted

How to calculate the closing speed of two objects

You essentially have two lines with the equations: \$a(t)=a_0 + t*v_a\$ \$b(t)=b_0 + t*v_b\$ where \$a_0, b_0, v_a, v_b\$ are vectors with the same dimension and unit (meters for \$a_0\$ and \$b_0\$...
LukeG's user avatar
  • 1,547
6 votes
Accepted

Direction vector to quaternion

From http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/ ...
Bram's user avatar
  • 3,719
6 votes
Accepted

Random Direction Vector Relative To Current Direction

Random direction relative to current direction: Assuming you already have an initial direction vector prepared, as per DMGregory's comment on the OP: Generate a ...
CosmicGiant's user avatar
  • 2,094
6 votes
Accepted

Is it possible to calculate a direction vector without sqrt?

If you want a correct mathematical result (correctly rounded to within floating point precision), a square root is the way to go. It's true that it's more expensive than a multiply, but it's still ...
DMGregory's user avatar
  • 135k
5 votes

How to get the closest "visible" object from my "player"?

You can use the dot product trick to get a list of instances actually visible by the player (thus lying inside the camera frustum), and check for the distance from your camera object to get the ...
liggiorgio's user avatar
  • 4,821
5 votes
Accepted

Does AddForce continue to add force indefinitely?

AddForce will apply a force for the current update cycle. The force is applied to the RigidBody for that update cycle, then cleared. At the beginning of each ...
House's user avatar
  • 73.2k
5 votes
Accepted

Find the closest point on a ray

Closest distance point segment: ...
Haruko's user avatar
  • 368
5 votes
Accepted

How to calculate vector of projectile initial velocity

Assume your variables are: initial velocity , initial position , and target position . First, choose your initial y-velocity, and calculate the time it requires to reach its target on the y-axis ...
WindyKeeper's user avatar
5 votes
Accepted

Need help with getting a direction vector between two given points

In your situation you need a simple subtraction, with a normalization. vectDistance = B - A vectDirection = vectDistance / lenght(vectDistance) This will give ...
Vaillancourt's user avatar
  • 16.3k
5 votes

Calculate vector3 global point projecting it in local space using Unity and C#?

Let's say you have a trasnformed coordinate system defined by a quaternion rotation, and vector3 position. For example, the ...
Theraot's user avatar
  • 27k

Only top scored, non community-wiki answers of a minimum length are eligible