Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 26, 2014 at 08:48 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Sendatsu_Yoshimitsu · Nov 26, 2014 at 04:51 AM · c#vector3dot

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sendatsu_Yoshimitsu · Nov 26, 2014 at 07:15 AM 0
Share

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.

avatar image Bunny83 · Nov 26, 2014 at 03:15 PM 0
Share

@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.

avatar image Sendatsu_Yoshimitsu · Nov 26, 2014 at 08:20 PM 0
Share

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

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet bounces off the wall 2 Answers

Having Trouble with Vector3.Angle 1 Answer

Modify mesh problems 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges