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
0
Question by diegonv · Jan 23, 2021 at 03:01 AM · rotationspaceshiparcturrets

Turret. Detect if target is inside horizontal and vertical arcs

Hi. I am trying to make a turret control for a spaceship (rts game). I have a coroutine that rotates the turret (a base horizontal axis, and a barrel vertical axis). It works fine, I have used Quaternion.Lookrotation and Quaternion.RotateTowards with a clamp to limit the minimum and maximum angle of both axes.

Then I have another coroutine that checks if there is a new target (the ship has two target lists: one holds ships added with right clicks (attack order), and the other adds ships when they are closer). If there is a target, it tells the first coroutine to target it.

To do this I need a function that returns a bool, depending on whether the target is within the turret arcs (both horizontal and vertical arcs). how can I do this? The arcs are relative to the ship and can go in all directions so it can tilt on all 3 axes.

The data I have is the transformation of the ship/horizontal turret, the position of the target, the minimum/maximum of both arcs.

I think it should make a projection of the target on the plane of the ship and? Can you guide me with this? alt text

aa.png (17.3 kB)
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

2 Replies

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

Answer by CodesCove · Jan 23, 2021 at 08:31 AM

I wrote this long time ago.. it's part of my sight sensor but should work for your case (no guarantee :) )

  private bool IsInFOV(Transform fovTransform, UnityEngine.Vector3 targetPosition, float maxHorizontalDegrees, float maxVerticalDegrees)
         {
             //to local space
             UnityEngine.Vector3 sourceDirectionForward = fovTransform.InverseTransformDirection(fovTransform.forward);
             //to local space
             UnityEngine.Vector3 targetLocalSpacePosition = fovTransform.InverseTransformPoint(targetPosition);
             UnityEngine.Vector3 targetLocalSpaceXZDirection = new UnityEngine.Vector3(targetLocalSpacePosition.x, 0, targetLocalSpacePosition.z) - new UnityEngine.Vector3(0, 0, 0);
 
             float targetLocalDistance = UnityEngine.Vector3.Distance(UnityEngine.Vector3.zero, targetLocalSpacePosition);
             //this.distance = targetLocalDistance;
 
             float targetAngleV = Mathf.Rad2Deg * (Mathf.Atan(Mathf.Abs(targetLocalSpacePosition.y) / Mathf.Abs(targetLocalDistance)));
             float targetAngleH = UnityEngine.Vector3.Angle(targetLocalSpaceXZDirection, sourceDirectionForward);
             
             if ((targetAngleV <= maxVerticalDegrees/2) && (targetAngleH <= maxHorizontalDegrees/2)) return true;           
             
             return false;
         }

Hope you get it working,,

PS. here are parameter descriptions:

fovTransform = transform of the eye

targetPosition = position of the target

maxHorizontalDegrees = Maximum of horizontal (max 360) angle between eye and target in degrees

maxVerticalDegrees = Maximum of vertical (max 180) angle between eye and target in degrees

Comment
Add comment · Show 1 · 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 diegonv · Jan 23, 2021 at 09:04 PM 0
Share

alt text Thank you, it works perfect. Im only changed the final line, because i use a $$anonymous$$/max for horizontal and a $$anonymous$$/max for vertical. I used something like this:

 return targetAngleH < maxHorizontalDegrees && targetAngleH > $$anonymous$$HorizontalDegrees && targetAngleV < maxVerticalDegrees && targetAngleV > $$anonymous$$VerticalDegrees;

asdasd.png (28.7 kB)
avatar image
0

Answer by diegonv · Jan 23, 2021 at 09:15 PM

If anyone is interested in how to rotate the turret, i use this (the objetive assignment is another topic).

  1. turret_horizontal is the transform of the horizontal axis of the turret (the body).

  2. turret_vertical is the transform of the vertical axis of the turret (the canon)

  3. turret_vertical must be child of turret horizontal

    void FixedUpdate() //or a coroutine with yield return new WaitForFixedUpdate { Quaternion _rot = Quaternion.LookRotation(targetTransform.position - turret_horizontal.position);

      turret_horizontal.rotation = Quaternion.RotateTowards(turret_horizontal.rotation, _rot, _vel_horizontal * Time.fixedDeltaTime);
         turret_horizontal.localEulerAngles = new Vector3(0f, fClampAngle(turret_horizontal.localEulerAngles.y, _ang_min_horizontal, _ang_max_horizontal), 0f);
         
         turret_vertical.rotation = Quaternion.RotateTowards(turret_vertical.rotation, _rot, _vel_vertical * Time.fixedDeltaTime);
         turret_vertical.localEulerAngles = new Vector3(fClampAngle(turret_vertical.localEulerAngles.x, _ang_min_vertical, _ang_max_vertical), 0f, 0f);
         
     }
     
     
     public static float fClampAngle(float angulo, float min, float max) //it clamps an angle without euler angles errors
     {
         angulo = Mathf.Repeat(angulo, 360f);
         return (angulo > 180f) ? Mathf.Max(angulo, 360f+min) : Mathf.Min(angulo, max);
     }
    
    
    
    
    
    
    
    
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

152 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 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 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 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 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 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 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 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

Getting Bullets to fire in Direction of Gun Barrel 1 Answer

How do I create a spaceship 1 Answer

Getting Bullets to fire in Direction of Gun Barrel 1 Answer

Space ship controls locking into place 1 Answer

Aiming a turret of arbitrary orientation 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