# Dot product not working as expected

Recently I am learning dot product,

While below code works from Unity's documentation, http://docs.unity3d.com/ScriptReference/Vector3.Dot.html

 public Transform other;
void Update() {
if (other) {
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toOther = other.position - transform.position;
var dot = (Vector3.Dot(forward, toOther);
Debug.Log(dot);

}
}


But it doesn't work when I change direction of "other".It keeps giving same value no matter how much I rotate other unlike when I rotate transform,it does change values.

For example, transform is at 0 0 0 and other is at 0 0 10 both has 0 rotation so facing straight in Z axis.

The desiire out put I get is negative.

Now suppose I change rotation of transform to 180 then the output gets positive as expected.

I undo the rotation of trasnform and rotate other to 180 i.e., facing transform, then the output doesn't get change and show negative value only.Which is not correct as other is facing player..

Why is that the case? I am not able to get around it.

• It sounds like you've misunderstood what's happening here. You're taking the dot product of transform.forward and the offset or change in position from transform to other. This answers the question "how far in front of me is other?" — this has nothing to do with the direction other is facing. If you want to answer the question "to what extent is other facing the same direction I am?" then you want Vector3.Dot(transform.forward, other.forward) – DMGregory Apr 6 '16 at 21:15
• Thank you taking time ot replay, does it mean I shall perform dot calculation two times?? ,first to check if enemy is ahead or behind and then calculate enenmy's direction? This is how shall I implement "backstabbing"? I was learning from this series...following video he is using dot product to apply hit if and only if player attack from behind of enemy. Accoding to dot product rule, if enemy changes its direction will the following example fail? youtu.be/HXpSQ7yyu3o – idurvesh Apr 7 '16 at 8:00
• Ok I get it we have to pass forward of enemy to dot product var a = player.position - enemy.position; a = a.normalized; Vector3.Dot(enemy.forward, a); – idurvesh Apr 7 '16 at 9:33
• It's still not clear to me what question you're trying to use this dot product to answer. Can you tell us more about the gameplay you're creating? If your gameplay relies on both facing and positioning information then yes you might need to compute two or more dot products (not a big deal — these are cheap operations, so it's worth using as many as you need to get correct, clear code) – DMGregory Apr 7 '16 at 13:03
• I am just learning vector operations in detail with examples, I was having trouble understanding how to do backstab i.e., sneak behind enemy and attack him... with help of your code I was able to solve it by doing var a = player.position - enemy.position; a = a.normalized; Vector3.Dot(enemy.forward, a) where we pass enemy direction instead of player to know at which direction player is looking.. – idurvesh Apr 7 '16 at 16:36