Da Hasi
Da Hasi

Reputation: 51

Unity - Math question about Vector calculation

i am looking for the mathematical formula for the following problem (in 3 dimensional space, so it is about 3 dimensional vectors). I am programming in Unity - GameEngine (C#)

In the first picture you can see my current result. I create 2 vectors (red spheres) and automatically generate an anchor point (green in the picture). this anchor point is always generated "behind" the 2nd vector (on a straight line). The two vectors are always the current and the immediately previous vector. My current calculation (first picture) looks like this:

 Vector3 direction = newVector.position - previousVector.position;
 Vector3 newAnchor.position = newVector.position + direction;

What i need is shown in the 2nd picture. I want the anchor point (green) (relative to the direction of the two vectors) to be generated always 90 degrees "to the right" of the 2nd vector. however, i can't derive the mathematical calculation for that.

Hopefully I could describe with the pictures what I try to achieve.

First picture:

First picture:

Second picture:

Second picture:

Upvotes: 1

Views: 860

Answers (2)

Iggy
Iggy

Reputation: 4888

2D

In order to rotate a vector by 90 degrees all you have to do is switch the x and y components and negate one of them. You can make extension methods, which allows you to call vector.Left() & vector.Right() directly on the vector.

public static class VectorX
{
    public static Vector2 Left(this Vector2 v)
    {
        return new Vector2(-v.y, v.x);
    }

    public static Vector2 Right(this Vector2 v)
    {
        return new Vector2(v.y, -v.x);
    }
}

3D

You can use the Cross Product, which returns a vector that is perpendicular to the other two.

var right = Vector3.Cross(forward, up).normalized;

So if you give the forward vector and the up vector of a point you will receive a vector pointing to the right. You can negate it if you want it pointing left.

Upvotes: 1

Jaibeer Dugal
Jaibeer Dugal

Reputation: 67

Hey I am guessing you want to basically find a particle that is in the same direction as the vector in front of it and then have it be perpendicular to the vector at a unit distance.

I suggest using a raycast forward to that vector with a required or a capsule cast.

here is the pseudocode i suggest

        public void FixedUpdate()
        {
//https://docs.unity3d.com/ScriptReference/Physics.CapsuleCast.html please use whatever overload you find comfortable 
                   if (Physics.CapsuleCast(p1, p2, charContr.radius, transform.forward, out hit, 10))
    {
                        distanceToObstacle = hit.distance;
               // Then get the ortho to the vector of the using cross product 
             Vector3 direction = newVector.position - previousVector.position;
             Vector3 right = -(Vector3.Cross(direction, Vector3.up).normalized);
             
             // now you have the ortho right of the vector now a new position should be 
             // the vector you need.. hopefully this helps 
             
    }
        }

PS: new here will work on haveing the editor open and give more concrete code soon

Upvotes: 0

Related Questions