- Home /
Dot Product of Vector
I am new in unity development.And i have searched many forums for Vector Dot product. I could not understand what is the basic purpose of dot product ,Is it for direction matching of two game objects thanks for help in Advance it shall be highly appreciated
Answer by robertbu · Aug 16, 2014 at 07:32 PM
The dot product is the magnitude of the projection of one vector on another. It is a basic vector concept that is used in a variety of places, like projecting a point on a plane, or figuring out what side of a plane a point is on. In Unity Answers we primarily see it used to detect direction.
Given two normalized vectors, the closer they are aligned, the closer the Dot product will be to 1.0. If they were at right angles to each other, then the value would be 0.0. A value of -1.0 indicates the vectors are opposite of each other. Note that it is not a linear progression. That is a value of 0.5 is not mean the two vectors are 45 degrees away from each other. If you need a linear progression, use Vector3.Angle().
For example, say you had a pair of dice and you want to check each one for which side was up. You could use Vector3.Dot() with each of the transform sides (transform.forward, transform.right, transform.up) against Vector3.up. A value near 1.0 would indicate that side was up. A value near -1.0 would indicate the opposite side was up. If none of the values were near 1.0, you would know the die was not resting flat on a surface.
Thanks For Reply How can i use it for Car parking? Actually i am working on car parking game in which i want parking when car and parking are in same direction . I used Position for it but it is not working in my case.
here is my Code snippet void Update () { Vector3 current = transform.TransformDirection (Vector3.down); if(Physics.Raycast(transform.position , current ,out hit , 30)) { if(hit.collider.gameObject.name == "Parking") { if (Vector3.Dot(transform.position , hit.collider.transform.position) > 0) { Debug.Log("samse direction"+Vector3.Dot(transform.localPosition.normalized , target.transform.position)); }
}
}
}
Two issues. First, you need to use a direction, not a position. So it would be something like:
Vector3.Dot(transform.forward, hit.transform.forward)
So this compares the forward of both vehicles. Next, I'm not sure what you want for a value. Using 0.0, means they are anywhere withing 90 degrees of each other. I don't know your game, but I would think you would want a higher value:
if (Vector3.Dot(transform.forward , hit.transform.forward) > 0.8f) {
P.S. For future posts of code, please select your code after pasting and use the 101/010 button to format it.
Thanx alot Next time i will post code in the code section thanx alot