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 JDeveloper · Jun 18, 2013 at 12:16 AM · anglevectorturretcircletower-defense

Making a Turret Have a Circle Sector Range

In the game I'm developing, a turret has a range shaped like a sector of a circle. The turret has an initial forward vector, and what I need is to have the turret only target enemies within 54 degrees (On the XZ plane) of that forward vector. The y axis is irrelevant here. How can I do this?

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

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jun 18, 2013 at 12:30 AM

If you know the enemy positions, you can compare the direction vector turret->enemy to the initial forward vector. Supposing that forwardDir has the initial forward direction, you can use a function like this (turret script):

 function EnemyInRange(enemy: Transform): boolean {
   // get direction vector:
   var dir: Vector3 = enemy.position - transform.position;
   dir.y = 0; // make it strictly horizontal
   forwardDir.y = 0; // make forwardDir stricly horizontal too
   // return false if outside angle...
   if (Vector3.Angle(dir, forwardDir) > 54) return false; // check angle...
   // or out of range
   if (Vector3.Distance(enemy.position, transform.position) > range) return false;
   // otherwise return true
   return true;
 }

HINT: A simple way to keep all enemies in a fast and always updated array is to child them to an empty object: create the empty object, call it Enemies and reset its position/rotation, then child every enemy to it right after it's created. You can iterate through them with a code like this (turret script):

 var enemies: Transform; // drag the Enemies object to this field
 
   ...
   for (var enemy: Transform in enemies){
     if (EnemyInRange(enemy)){
       // enemy inside range: shoot the bastard!
     }
   }
   ...
Comment
Add comment · Show 2 · 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 JDeveloper · Jun 19, 2013 at 10:44 PM 0
Share

Thanks for helping, but I already tried this and it doesn't work. Just to be sure I hadn't missed something, I tried your code, but it still doesn't work.

avatar image robertbu · Jun 20, 2013 at 12:26 AM 0
Share

@aldonaletto's code looks just fine (as it almost always is). I ran a quick test just to make sure, and it worked fine for me. It assumes you setup forwardDir correctly. It needs to be the forward of your selector in world coordinates. If you selector is in front of the camera, then you can use the camera forward (Camera.main.transform.forward).

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

Rotation circle to stop at a certain angle 1 Answer

Vector to angle 3 Answers

Rotation Angle across 0, opposing angle? 1 Answer

Need to know euler angles of Vector3 direction relative to other Vector3 direction 1 Answer

Arbitrary rotation around a point works except when up flips? 1 Answer


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