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…

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

2 Answers

2

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.

You'd want to add it after you decide the player has decided to fire and before you actually fire it.

lower case f: http://docs.unity3d.com/Documentation/ScriptReference/Mathf.html

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?

I don't quite know how to use the script suggested and where to place it… what value is firing vector? It's not really in my script.

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

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