2022 Developer Survey is open! Take survey.

# Tag Info

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: ...
• 3,162

### How can I draw a line of certain length and direction?

You can simply get the vector pointing in the right direction, then scale it by the distance you want and add that to the initial point. This will define a new end point for your line. ...
• 72.7k
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 ...
• 2,034
Accepted

### What direction does directional audio come from for a 2D view?

The spatial audio should be relative to the camera. If you consider it, even in a 3D game, the audio is relative to the camera. Sounds that come from the right side of the screen are heard from the ...
• 72.7k
Accepted

### Unity3d: Find which gameObject is in front

You can check simple ahead/behind relationships using the dot product: ...
• 113k
Accepted

### How to align a hint arrow to point along a path?

It looks like your trouble isn't in computing the direction - it's that your arrow asset was imported with an unusual orientation, so when you use methods like LookAt or LookRotation they give you the ...
• 113k

### How can I calculate force and acceleration?

Every frame: acceleration <-- from input velocity = velocity + acceleration * t position = position + velocity * t ...
• 1,444
Accepted

### How to make an enemy projectile head to the player's position with constant speed

IF I understood your question correctly (it's a bit un-detailed and I recommend editing it with more information and the piece of the code that you tried but is not working), you can solve the problem ...
• 4,885
Accepted

### How to get direction and velocity of movement of an object dragged with the mouse in Unty (C#)

The velocity is the difference between the new position and the last position. velocity = newPos - oldPos The vector direction is the normalized velocity. ...
• 1,708

### Movement velocity relative to a static object (Vector Maths)

The dot product between two unit vectors is: +1 when the vectors are pointing in the same direction 0 when the vectors are perpendicular -1 when the vectors are pointing in opposite directions So, ...
• 113k
Accepted

### How can I move an object in a direction other than the ordinals?

The white vector is the correct vector with the code you have. If you're only ever adding integers to your position, the movement is going to be at increments of 45 degrees. That's restricted to ...
• 72.7k
Accepted

### How can I modify this projectile class to support movement in an arbitrary direction?

What you have now is essentially: screenPosition.X -= velocity * 1.0; screenPosition.Y -= velocity * 0.0; You need to explicitly add a ...
• 10k
Accepted

### How can I calculate force and acceleration?

While the equations v(n+1) = v(n) + a d(n+1) = d(n) + (v(n+1) + v(n)) / 2 with: d(n) is position at time n; v(n) is velocity at time n; and a is acceleration in distance units per frame ...
• 2,101

### How do I set angular velocity/torque so that it's pointing to velocity/direction?

Summary My recommendation is to compute a restorative torque to apply to the object. This is physically more accurate than setting the velocity directly, and the simulation will be better behaved. ...
• 1,685

### How can I draw a line of certain length and direction?

If you know the start and end points as well as the desired length, you can use Vector3.Lerp ...
• 1,369

### Direction between objects (shortest distance around sphere)

A few issues come to mind as I read this: Your movement is on the surface of a sphere, so travel direction should be a tangent to this surface, not just a (to-from) normalized, as that would point ...
• 3,669
Accepted

### Raycast wrong direction

Currently your raycasting happens relative to your transform. Your transform is currently rotated so "forward" is now pointing downwards. If you want your raycasting to happens relative to the global ...
• 413
Accepted

### How to check if two normals/directions "look at" each other?

Given points P0 and P1 with normals N1 and N2... ...
• 3,669
1 vote
Accepted

### Bullet direction incorrect around sphere

transform.Rotate(x, y, z) rotates in local Euler angles. That means it applies a rotation of y degrees about the local y+ axis, ...
• 113k
1 vote
Accepted

### Make object face another object on a sphere

This can be done more simply than it might appear. We can think of this as an orientation that points our local y+ (up) axis directly outward from the sphere, so our local xz plane is tangent to the ...
• 113k
1 vote
Accepted

### Create rotation in random direction from existing rotation

For anyone who is curious, here is what I ended up doing. I created a new rotation using Quaternion.AngleAxis: ...
• 556
1 vote

### Random Direction Vector Relative To Current Direction

I've done this, and its not too hard. Firstly, generate a random non 0 length vector. Normalise it. Cross product with your current direction vector to create a rotation axis. Generate a random ...
• 1,100
1 vote
Accepted

### Calculating correct normal direction from multi-variable function

This trick Vector3 normal = new Vector3(tangent.y, 0, -tangent.x); works great in 2D, when we only have 2 perpendiculars to choose from. It's not the right ...
• 113k
1 vote

### Why is there a discrepancy between these two transform.up vectors?

I called the method that ran the Debug.LogError(obj.transform.up); inside a coroutine, the coroutine essentially had an off by one error and given that the object was moving in 90 degree steps and I ...
1 vote

### How to rotate a local position offset based on a direction vector?

Since it looks like you're working in the 2D x/y plane here, you can use a cute little trick: ...
• 113k
1 vote
Accepted

### Get GameObject which instantiated me in Unity

You must attach a script to the prefab you instantiate and "inject" a reference of the gameobject which has instantiated the prefab as follow : ...
• 2,909
1 vote
Accepted

### Flip x when direction of the movement changed in Unity

I suggest do not bind the flipping procedure to the Input since it is directly relevant to the moving direction of the body (Input may not change the body's moving ...
• 537
1 vote

### Collision resolution when moving in two directions

This is almost a comment, but too long so i'll post as an answer. Hopefully it will help. There's a design flaw i think in your code : you solve on x then on y but in both cases you set both x and ...
• 1,413
1 vote
Accepted

### 2D Physics: storing previous position vs storing velocity

Without knowing exactly the tutorials or books you have read, here is what I can tell you. To be more precise in terms of physics definition: the vector direction is calculated as the difference ...
• 4,885
1 vote

### How does this math behind converting angles to vectors work?

It works via repeated Rotations, you begin mentally with the Vector {1,0,0} then you rotate it along the Y-Axis the length of the vector is just one so you can get the new coordinates simply by ...

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