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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by JPB18 · Jun 05, 2013 at 02:40 PM · javascriptgameobjectfunctionanglefind

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)

Comment
Add comment · Show 18
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 LeMoine · Jun 05, 2013 at 02:58 PM 0
Share

If you debug.log the angle and the firing angle just before the test of in or out the angle, what comes out? I mean, should the test succeed? :D

avatar image JPB18 · Jun 05, 2013 at 03:15 PM 0
Share

Strange enough, when I rotate the ship around, no matter if I turn away from the target, the values don't change even a little bit...

I added this:

 Debug.Log(firingAngle/2 + " - " + angle);

And here's the starting results (when the ship is looking at the target) for weapon1:

"135 - 171.7471"

weapon2:

"45 - 130.8781"

weapon3 (which somehow is the only one checking the isAngle):

"135 - 129.9219"

Still, I don't know what makes results this different, for points with less than 0.1 units of distance between them...

avatar image robertbu · Jun 05, 2013 at 03:18 PM 0
Share

While I see a number of things that could be simplified, I don't see anything that is obviously wrong with your calculations. As @Le$$anonymous$$oine suggests, put in some Debug.Log() statements to verify all the inputs and calculations.

avatar image LeMoine · Jun 05, 2013 at 03:40 PM 0
Share

Debug.Log(origin) before the angle calculation?

avatar image JPB18 · Jun 05, 2013 at 03:55 PM 0
Share

With:

 Debug.Log("Origin[" + weapon.slot_num + "] position:" + origin.position.ToString() + "; rotation: " + origin.rotation.ToString());

And here's the result:

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)

Show more comments

1 Reply

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

Answer by JPB18 · Jun 05, 2013 at 05:58 PM

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.

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

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

Function to locate game object with smaller distance to target makes the game lag - UnityScript 1 Answer

Loading function into array using GameObject.Find 1 Answer

Find 2D Texture via Script to use in Static Function 2 Answers

Alternatives to GameObject.Find(); 1 Answer

Finding Children question 3 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