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 /
avatar image
1
Question by Julian-S · Jun 01, 2013 at 01:26 AM · playeraivectorfacingside

Enemy Broadside AI

So I've got the details of an enemy broadside laid out, the only problem being that I can't figure out a way to detect if the side of the enemy ship is facing the player target. I also need to know which of the two sides is facing the player target in order to do either a left broad side or right broad side depending.

I have been toying away for hours but I still can't come up with anything that functions.

Any help would be greatly appreciated!

Comment
Add comment · Show 2
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 OP_toss · Jun 01, 2013 at 01:43 AM 0
Share

What you need is to perform a dot product between the ship's forward vector, and the vector between the ship and your player target. This should be close to 90 in order for the player to be on the left or right side of the ship.

 float cosAngle = Vector3.Dot( ship.transform.forward, player.transform.position - ship.transform.position );
 
 float angle = $$anonymous$$athf.Acos( cosAngle ) * $$anonymous$$athf.Rad2Deg;
 
 if ( $$anonymous$$athf.Abs( angle - 90 ) < angleOfView )
     TargetPlayer();

Something like that should work. You can optimize further by removing the Acos, as cos(90) == 0. So you'd just have to know the cos of your angleOfView variable. I think.

Hope this helps!

avatar image Julian-S · Jun 01, 2013 at 06:40 AM 0
Share

Thanks! This is pretty much working out! How do I do as you suggested and remove the Acos? I figure that is what is causing a lot of the "NaN" I'm receiving.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Jun 01, 2013 at 01:34 AM

First your ship script will need access to either the player game object or the player transform. You can either link them in the inspector or use GameObject.Find() to initialize the variable. Once you have the variables you can do something like this:

 var v3ToPlayer = goPlayer.transform.position - transform.position;
 
 if (Vector3.Angle(v3ToPlayer, transform.right) < some_value) {
    // Do the right side facing stuff
 }  
 else if if (Vector3.Angle(v3ToPlayer, -transform.right) < some_value) {
    // Do the left side facing stuff
 }

'some_value' defines how much angle you will allow and still shoot the broadside.

Comment
Add comment · Show 6 · 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 robertbu · Jun 01, 2013 at 03:28 AM 0
Share

As your original code was trying to do, you could use the Dot product ins$$anonymous$$d:

 if (Vector3.Dot((v3ToPlayer, transform.right).normalized) > some_value) {
   // Do the right side facing stuff
 }

some_value here would be greater than 0. 0 is at 90 degrees.

avatar image Julian-S · Jun 01, 2013 at 06:38 AM 0
Share

Thanks. This is somewhat the approach I took previously and it almost works. The problem is that occasionally the ship side will be facing an angle that would satisfy it, but it is not facing the player. I'm not sure, it has weird behaviour still.

avatar image robertbu · Jun 01, 2013 at 06:55 AM 0
Share

This code calculates angles between vectors in 3D space. If you are shooting at flying objects, or if you ships are pitching and rolling, you may want to project both vectors down to a plane before doing the calculation. You can do that by:

 var v3ToPlayer = goPlayer.transform.position - transform.position;
 v3Player.y = transform.position.y;
 var v3 = transform.right;
 v3.y = 0;
 if (Vector3.Angle(v3ToPlayer, v3) < some_value) {
    // Do the right side facing stuff
 }  
avatar image Julian-S · Jun 01, 2013 at 06:56 AM 0
Share

I'll give this a shot and then let you know, thanks.

avatar image Julian-S · Jun 01, 2013 at 07:01 AM 0
Share

I believe that worked like a charm! I'll just finish up the code and see if I can get it running with the shots going off. Then -v3 for left side facing stuff I guess?

Also, why don't I have permission to thumbs up you?

EDIT: Yup, my ai boat is officially destroying me anytime it gets the opportunity to broadside. Thanks a ton robert, I love how helpful this community is

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

NavMesh flee. Ai flee from player. 4 Answers

How can make rank in racing game? 1 Answer

Keeping Character From Walking Through Walls 1 Answer

Smoke Follow Player 1 Answer

AI enemy script (same as in Hitman) 0 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