Finding the angle between two GameObjects

Hi there!

So I’m currently trying to do a function that checks if a certain GameObject (lets call it a target), it’s in the firing arch of a weapon. I’ve managed, with the help of the scripting guide, to come up with this:

//this function can be used to check if the target is withing the firing angle
function CheckWeaponAngle(weapon : WeaponSlot, target : Transform) : boolean {

	//get weapon script and firing angle
	var weaponGO : GameObject = weapon.weapon_go;
	var weaponScr : weaponScript = weaponGO.GetComponent(weaponScript);
	var firingAngle : float = weaponScr.firingAngle;
	
	//get weapon origin
	var weaponType = weaponScr.type;
	var origin : Transform;
	
	if(weaponType == WeaponType.beam)
	{
		origin = weapon.phaser_point.transform;
	}
	else if (weaponType == WeaponType.torpedo)
	{
		origin = weapon.torpedo_point.transform;
	}
	else if (weaponType == WeaponType.pulse)
	{
		origin = weapon.pulse_point.transform;
	}
	
	//get angle between the objects
	var targetDir = target.position - origin.position;
	var forward = origin.forward;
	var angle = Vector3.Angle(targetDir, forward);
	
	//check if it's inside the angle or not
	var isAngle : boolean;
	if (angle <= firingAngle/2)
	{
		isAngle = true;
	}
	else
	{
		isAngle = false;
	}
	
	//return result
	return isAngle;
	
	
}

The “class” weaponScript is pretty much a script which contains informations about the weapon type, it’s firing angle, etc…
And this is the “WeaponSlot” class

class WeaponSlot {
	var isEnabled : boolean = false; //checks if the weapon is enabled
	var slot_num : int; //number (still has no function)
	var weapon_go : GameObject; //weapon GameObject. It contains the projectile
	var orientation : weapon_orientation; //checks if the weapon is firing forward or backwards
	var phaser_point : GameObject; //if the weapon is a beam weapon, it fires from this game object
	var torpedo_point : GameObject; //if the weapon is a torpedo weapon, it fires from this game object
	var pulse_point : GameObject; //if the weapon is a pulse weapon, it fires from this game object
	var nextShot : float = 0.0f; //contains the time reference for when the weapon is able to fire again
	var isAngle : boolean = false; //checks if the target is inside the firing arch
	var isRange : boolean = false; //checks if the target is in range
	
}

And the function is applied by using:

if(weapon1.isEnabled && weapon1.weapon_go != null)
	{
		weapon1.isAngle = CheckWeaponAngle(weapon1, shipTar.transform);
	}

Weapon1 is an object with the structure of the WeaponSlot class. This first checks if the weapon game object exists and if the weapon is enabled.

HOWEVER, even though I’m currently only controlling 3 forward facing weapons, each with a rotation of (0,0,0) and subordinated to the same parent object, which has the same rotation of (0,0,0), (and the same until the master parent, which has a rotation of (0,0,180)), the “isAngle” property of weapon1, weapon2 and weapon3 won’t change, no matter how much I rotate my ship (a.k.a. master parent GameObject).

So, any tip on how can I solve this problem?

EDIT:

Here’s the result of Debug.Log(firingAngle/2 + " - " + angle):

  • weapon1: “135 - 171.7471”
  • weapon2: “45 - 130.8781”
  • weapon3 (which somehow is the only one checking the isAngle): “135 -
    129.9219”

And here’s the result of Debug.Log(“Origin[” + weapon.slot_num + “] position:” + origin.position.ToString() + "; rotation: " + origin.rotation.ToString()):

  • Origin[1] position:(105.0, 0.0, 0.0); rotation: (0.0, 0.0, 1.0, 0.0)
  • Origin[2] position:(105.0, 0.0, 0.3); rotation: (0.0, 0.0, 1.0, 0.0)
  • Origin[3] position:(104.8, 0.0, 0.1); rotation: (0.0, 0.0, 1.0, 0.0)

For: Debug.Log(“targetDir[” + weapon.slot_num + "]: " + targetDir.ToString() + “; forward[” + weapon.slot_num + "]: " + forward);

  • targetDir[1]: (0.0, 0.0, 0.0); forward[1]: (0.0, 0.0, 1.0)
  • targetDir[2]: (0.0, 0.0, -0.3); forward[2]: (0.0, 0.0, 1.0)
  • targetDir[3]: (0.2, 0.0, -0.1); forward[3]: (0.0, 0.0, 1.0)

So, the problem after all was a variable naming. Up in the variable declaration I had the following part:

var shipTar : shipTarget;
var shipTarg : GameObject;

I then had a script that would fetch the target ship and assign it to shipTarg. However, when calling out the above CheckWeaponAngle() function, I called up the ShipTar script, instead of calling out the shipTarg game object. So instead of:

if(weapon1.isEnabled && weapon1.weapon_go != null)
    {
       weapon1.isAngle = CheckWeaponAngle(weapon1, shipTar.transform);
    }

I should’ve made:

if(weapon1.isEnabled && weapon1.weapon_go != null)
    {
       weapon1.isAngle = CheckWeaponAngle(weapon1, shipTarg.transform);
    }

I apologize to LeMoine and robertbu for wasting time on a perfectly fine script, and I also thank them for the help provided.