- Home /

**closed**Nov 26, 2014 at 08:48 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

# Can't figure out why dot product isn't facing the direction I think it will

I'm trying to make it so when the player hits attack, they swing (and check for targets) in the direction they're holding the WSAD keys/analog stick. So if the player hits attack while holding D, the game will check for hits in the cone starting in the model's transform.right:

for (int i = 0; i < defenderList.Count; i++){ //Iterate through the list of enemies that are in range

```
if (Input.GetAxis ("Horizontal") == 0 && Input.GetAxis ("Vertical") == 0){ //If the player isn't holding any movement key
playerAttackAngle = Vector3.Dot(directionToTarget, transform.forward); //Take the dot product of the direction to target + player's forward
}
else{ //If the player IS holding a movement key
playerAttackAngle = (Vector3.Dot(directionToTarget, new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")) ) ); //Take the dot product of the direction to target plus the direction of the vector formed by the WSAD keys the player is holding
}
if (distanceToTarget <= attack.range && (playerAttackAngle > attack.areaOfEffect)){ //Double check that enemy is in range, and inside the player's attack cone
//Message enemy that they've been hit
}
}
```

However, I can't seem to make this work: enemies are correctly hit if I stand in front of them and don't move, but when I hold any direction, enemies in the direction I'm holding don't register the hit. Am I doing my vector math wrong?

**Answer** by Bunny83
·
Nov 26, 2014 at 06:11 AM

Well, your problem is probably that you did not normalize both vectors, did you? The dot product is literally just:

```
public static float Dot(Vector3 V1, Vector3 V2)
{
return V1.x * V2.x + V1.y * V2.y + V1.z * V2.z;
}
```

So to use the result as cos(alpha) the vectors need to be normalized. GetAxis might return a value smaller than 1 (since it's slowly increasing, depending on the axis settings in the Input manager) also if you press both directions at the same time, both axis will be 1, so the length of your vector will be sqrt(2) which will also give you a wrong Dot-product result.

Simply use ".normalize" after each vector when performing the dot product:

```
playerAttackAngle = Vector3.Dot(directionToTarget.normalized, (new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"))).normalized );
```

Finally, since we don't know what value is stored in "attack.areaOfEffect", the reference value need to be something smaller than 1 since cos(0°) == 1 and cos(90°) == 0. If you really have an actual angle in "attack.areaOfEffect" you have to use Mathf.Acos to get the angle. In this case keep in mind that Mathf.Acos returns the angle in radians not in degree. To convert it to degree you can simply multiply the result by Mathf.Rad2Deg

So I feel silly, this fixes the problem for the most part, although it still doesn't seem to detect enemies that are extremely close to the player. I'll keep working on tuning this and making it perform better, but for the record is using the dot product to trace a cone of effect an inherently wonky way to go about short-range combat? I'm trying to go for an assassin's creed/arkham asylum sort of loose lock on, where the game uses the player's directional input to suss out which enemy he's trying to aim for, but this feels a little clunky thus far.

@Sendatsu_Yoshimitsu:

I guess your directionToTarget has an y value, right? So the pivot of you and your target isn't on the same level. That's why the vector points up or downwards. So the closer the enemy gets the steeper the vector will point up or down.

To fix this you should set the y component of directionToTarget to "0" before normalizing. So you effectively have two "2d" vectors.

Awesome. that is much cleaner- thank you very much :)

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Instantiate object in C# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Having Trouble with Vector3.Angle 1 Answer