# Dot product not dot producting

**URL:** <https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093>\
**Category:** Blueprint\
**Tags:** unreal-engine, Blueprint, question\
**Created:** [June 17, 2024, 8:59am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093 "2024-06-17T08:59:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HawksHalo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hawkshalo/32/949854_2.png) [@HawksHalo](https://forums.unrealengine.com/u/HawksHalo)\
**Post date:** [June 17, 2024, 8:59am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/1 "2024-06-17T08:59:10Z")

</div>

I’ve attached images of my current dot product code and the lines I’m trying to work out the angle between. I keep trying to remake this or make it work based on tutorials but somehow I just keep making it worse.

I’m trying to work out if the player is over 40 degrees to the right or left of the turret, and I have to use dot product to do this because it’s for an assessment, but the math just isn’t mathing.

Am I doing something wrong???

  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/d/6/6d66e80efc1c743f2f987f979343703ded2fd98d.png)

Edit, made a few adjustments to the code, it’s still not working but I think I’m 20% closer??

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/c/b/4cb4b8649076ee89a9fe1133d38455b4b78138eb.png)  

Except now it thinks the right angle from the character is 0 degrees and I have no idea how I can combat this in code aa

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 17, 2024, 9:13am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/2 "2024-06-17T09:13:27Z")

</div>

> the math just isn’t mathing

- no need to normalise `forward`, it’s already normalised
- the 2nd vector is incorrect:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/9/f4982cde5785e649173b7c10497020f140e0f982.png)

This 👆 is direction to the player from world origin 0,0,0 and does not account for where the turret is. Instead of player location, do `Unit Direction` from `Turret World Location` to `Player World Location` and feed that into the `dot` node:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/6/4/164c93cec5b90f7b5ed95dffbbd2afc1e588f351.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 17, 2024, 9:21am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/4 "2024-06-17T09:21:05Z")

</div>

No need to normalise Unit Direction - already normalised as well. Your pic seemed OK at a (very quick) glance, though! In case the above does not produce what it should:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/3/d/63d7b0019a8275b1c53427afc8733166090438e6.png)

Do double check how this is rotated.

---

<div class="post-metadata">

**Author:** ![HawksHalo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hawkshalo/32/949854_2.png) [@HawksHalo](https://forums.unrealengine.com/u/HawksHalo)\
**Post date:** [June 17, 2024, 9:23am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/5 "2024-06-17T09:23:50Z")

</div>

Sorry deleted the last one since I noticed you were still updating it and didn’t want to get confused in the process-

I have this now but I’m still not sure if it’s 100% accurate?? Idk too much for my brain too late at night

  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/5/f/c5f34135e03f0bd56250c1c40f405e9c70d8be8d.png)

Also is there a specific way I should check if it’s in the target range of 40 degrees? I get the feeling I may not be doing that right either

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 17, 2024, 9:27am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/6 "2024-06-17T09:27:29Z")

</div>

> I have this now but I’m still not sure if it’s 100% accurate?

You may need to elaborate on that.

* * *

You could try:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/2/d/b2d2fa089c2c1677a78aa3bfd356245cea89ad71.png)

Shaves some nodes off the script.

* * *

Not exactly sure what the ideal end result is but you could try this slightly more versatile approach:

> [@Restrict Pawn's Rotation](https://forums.unrealengine.com/t/restrict-pawns-rotation/487856/3):
>
> Have a look at this one: Script to copy: [[Image from Gyazo]](https://gyazo.com/07facc8d174c94dd6103a641878c6929) Essentially, Dot Product is an answer to most angular issues. The yellow arrow indicates the turret’s front. Oh, and a TL is probably not the way to go about it unless you’re after something truly specific / unique / inflexible. Does not mesh well with target tracking in my head.

---

<div class="post-metadata">

**Author:** ![HawksHalo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hawkshalo/32/949854_2.png) [@HawksHalo](https://forums.unrealengine.com/u/HawksHalo)\
**Post date:** [June 17, 2024, 9:33am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/7 "2024-06-17T09:33:24Z")

</div>

Idea end result is kind of like an angle of attack where the turret can spot the player. If they’re in between 40 degrees left to right in front of it, it should be able to spot them.

Just tried the in range you suggested and it worths perfectly so thanks, you’re a sanity saver- I can sleep in peace tonight 🥲

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [June 17, 2024, 9:35am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/8 "2024-06-17T09:35:13Z")

</div>

> [@HawksHalo](#):
>
> If they’re in between 40 degrees left to right in front of it, it should be able to spot them.

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/4/c/24c0ef495401d678a250e515102c773d0bda2f62.png)

- dot returns -1 : 1
- 0-1 when vectors do align
- it cannot produce -140 in this very case

Try:

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/1/8/c18037f846c4ee5e1204663ab75a4a8247ad4302.png)

But you may need transforms (as in the other thread) - depending on how relative attachments are made.

---

<div class="post-metadata">

**Author:** ![HawksHalo](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hawkshalo/32/949854_2.png) [@HawksHalo](https://forums.unrealengine.com/u/HawksHalo)\
**Post date:** [June 17, 2024, 9:36am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/9 "2024-06-17T09:36:07Z")

</div>

Yep that worked, thanks again!

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [July 17, 2024, 9:36am UTC](https://forums.unrealengine.com/t/dot-product-not-dot-producting/1906093/10 "2024-07-17T09:36:55Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
