Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Carbongrip · Nov 12, 2013 at 11:16 PM · c#targetingdegreeszonebeam

Targeting Arc in Degrees

Is there some way I can adapt my current script to only fire in a forward facing 250 zone? How about a aft version? So basically I want to check and make sure my selected target is within a 250 degree targeting zone based on the forward section of the ship, if not in arc then abort the firing sequence. I will also need to mod that for a aft one also which shouldn't be to hard. Here is my current script…

 using UnityEngine;
 using System.Collections;
  
 [RequireComponent(typeof(LineRenderer))]
  
 public class BeamArray : MonoBehaviour 
 {
     LineRenderer line;
     public Material lineMaterial;
     bool phaser = false;
     public AudioClip sound;
  
     void Start()
     {
           line = GetComponent<LineRenderer>();
            line.SetVertexCount(2);
            line.renderer.material = lineMaterial;
            line.SetWidth(0.1f, 0.25f);
         line.enabled = false;
     }
     
     void Update()
     {
         var script = GameObject.Find("aaMain Camera").GetComponent<SelectTarget>();
         if(Input.GetKeyDown("space") && phaser == false && script.gui == "yes")
         {
             phaser = true;
             StartCoroutine (pulsetime());
         }
     }
         IEnumerator pulsetime()
         {
             var script = GameObject.Find("aaMain Camera").GetComponent<SelectTarget>();
               line.enabled = true;
             AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
               RaycastHit hit;
                if (Physics.Linecast(transform.position, script.selectedTarget.transform.position, out hit))
               {
                 if(script.Shields == false)
                 {
                     print("Shields Down, damage going to hull!");
                        hit.transform.SendMessage("ApplyDamage", 700, SendMessageOptions.DontRequireReceiver);
                 }
                 if(script.Shields == true)
                   {
                       // vector from the target center to the hit position
                        Vector3 hitDirection = hit.point - script.selectedTarget.transform.position;
                        Transform targetTransform = script.selectedTarget.transform;
 
                        float angleForward = Vector3.Angle(hitDirection, -(targetTransform.up));
                        float angleBack = Vector3.Angle(hitDirection, targetTransform.up);
                        float angleRight = Vector3.Angle(hitDirection, targetTransform.right);
                        float angleLeft = Vector3.Angle(hitDirection, -(targetTransform.right));
                        float angleTop = Vector3.Angle(hitDirection, targetTransform.forward);
                        float angleBot = Vector3.Angle(hitDirection, -(targetTransform.forward));
 
                        string shieldHit = "";
                        float minAngle = 180;
 
                        if (angleForward < minAngle)
                     {
                          shieldHit = "ApplyShieldDamageForward";
                          minAngle = angleForward;
                        }
 
                        if (angleBack < minAngle)
                     {
                          shieldHit = "ApplyShieldDamageAft";
                          minAngle = angleBack;
                        }
 
                        if (angleRight < minAngle)
                     {
                          shieldHit = "ApplyShieldDamageStarboard";
                          minAngle = angleRight;
                        }
 
                        if (angleLeft < minAngle)
                     {
                          shieldHit = "ApplyShieldDamagePort";
                          minAngle = angleLeft;
                        }
 
                        if (angleTop < minAngle)
                     {
                          shieldHit = "ApplyShieldDamageDorsal";
                          minAngle = angleTop;
                        }
 
                        if (angleBot < minAngle)
                     {
                          shieldHit = "ApplyShieldDamageVentral";
                          minAngle = angleBot;
                        }
                     
                     hit.transform.SendMessage(shieldHit, 700, SendMessageOptions.DontRequireReceiver);
                 }
             }
             //wait the cooldown time in a loop
             float delay = 1.680f;
             while (delay > 0)
             {
                 //update beam endpoints
                 if(script.Shields == false)
                 {
                     line.SetPosition(0, transform.position);
                      line.SetPosition(1, script.selectedTarget.transform.position);
                 }
                 if(script.Shields == true)
                 {
                     line.SetPosition(0, transform.position);
                      line.SetPosition(1, hit.point);
                     script.selectedTarget.FindChild("Shield Bubble").GetComponent<MeshRenderer>().enabled = true;
                 }
                 //decrement delay time
                 delay -= Time.deltaTime;
                 //let unity free until next frame
                 yield return null;
             }
                 line.enabled = false;
                 phaser = false;
                 script.selectedTarget.FindChild("Shield Bubble").GetComponent<MeshRenderer>().enabled = false;
         }
 }

I am feeling so silly with all my complex questions… lol all just learning...

Comment
Add comment · Show 1
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 Dracorat · Nov 13, 2013 at 09:47 PM 1
Share

No one wants to read through 160 lines of code, most of which appear to have nothing to do with the question at hand. $$anonymous$$aybe if you pruned your code to the relevant bits, you'd get more people responding.

2 Replies

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

Answer by undead-steve · Nov 14, 2013 at 01:16 AM

If what your asking is how to figure out if a vector is within a range (ie, +/- some number of degrees from your transforms's local forward, you're already using Vector3.Angle the right way. Something like

 if (Vector3.Angle(transform.forward, firingVector) <= 125)
 {
    //fire
 }
 else
 { 
    // play 'out of firing arc' sound
 }

Seems to be the idea - though you should probably revisit your question for clarity.

Comment
Add comment · Show 13 · 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 Memige · Nov 18, 2013 at 10:37 PM 1
Share

Based on your question, Undead-steve's answer looks correct, even after your clarifications. Are you not sure how to implement what he suggested, or is it not what you were looking for?

avatar image Memige · Nov 19, 2013 at 01:18 AM 1
Share
 script.selectedTarget.transform.position - transform.position

will give you the vector from your player to the enemy, thus the firingVector. I would recommend placing it just before you do the ray cast hit check (No reason to do an expensive ray cast if they aren't in firing position) Basically, if they are outside of the firing arc, yield break; to cancel the firing attempt. To do the same thing for firing backward, just reverse the forward vector by doing transform.forward * -1

avatar image Memige · Nov 19, 2013 at 03:27 PM 1
Share

Change

if ($$anonymous$$athf.Abs(Vector3.Angle(transform.forward, script.selectedTarget.transform.position - transform.position)

to

if ($$anonymous$$athf.Abs(Vector3.Angle(transform.forward, script.selectedTarget.transform.position - transform.position))

You had your ')' in the wrong place, and your want 125 because it is going to calculate it off of that amount from the center so 125 to the left and 125 to the right, for a total of 250 arc

avatar image Memige · Nov 20, 2013 at 10:21 PM 1
Share

reset phaser to false just before the yield break

avatar image Memige · Nov 21, 2013 at 02:34 AM 1
Share

Thanks and glad I could help. Would you $$anonymous$$d marking undead-steve's answer as correct? $$anonymous$$y clarifications may have helped you understand it, but he did give the correct answer. And that way people with similar questions can more easily find this conversation chain :)

Show more comments
avatar image
0

Answer by Inkarr · May 06, 2021 at 08:38 PM

@Carbongrip This thread is so old now that i'm not sure how active you still are, but it looks like you were creating something very similar to what i want to do. SFC3 or simiar by any chance?

Comment
Add comment · 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

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

20 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

lookat wont track target 1 Answer

Targeting script not finding a target 2D Mode 1 Answer

how can i auto target Gameobject on startup 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