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 DeadKenny · Feb 12, 2015 at 12:12 AM · rotationtransformquaternioneuleranglesslerp

How to get Angle float of specific axis.(Turret clamping related).

This is related to my previous question: http://answers.unity3d.com/questions/898185/how-to-clamp-the-rotation-of-this.html

Basically I am trying to clamp my barrel on a turret which slerps to a raycast hit by the camera. Everything I try to clamp it just messes it up. The closest I have come however is to get the direction angle of the turret to camera aim. So if its above a certain amount it won't use lookrotation and slerp. However this is very clunky and I have to set it high makes the barrel turn on all axis.

So what I am trying to do now is get the degrees of the turret facing to the camera target on the x/z and y axis separately.

This is the basic code from the previous link.

What I need is to get two angles eg: angleB and angleA from the direction of the turret. In the code aimBAngle does such a thing but its on all axis. I need to get two separetely.

     aimTDir = transform.InverseTransformPoint(aimTarget.position);
  
      aimBAngle = Vector3.Angle(aimTDir, -transform.forward);
  
          if(aimBAngle < 50.0f){            
           Quaternion bRot = Quaternion.LookRotation(aimHitPoint - barrel.position);
  
           barrel.rotation = Quaternion.Slerp(barrel.rotation, bRot, Time.deltaTime * 5.0f);
              }
 


 

Hope you understand... I also appreciate any other methods to solve this. Thanks. Its causing me great grief, so please help.

My game WIP: http://forum.unity3d.com/threads/omega-void-wip.200629/

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
0
Best Answer

Answer by DeadKenny · Feb 13, 2015 at 01:07 PM

Solved it.

So the main part is angleB1 and angleB2. They calculate the x and z direction of the barrel and then only allows the slerp to target if within this range. aimB is a 3rd angle check directly from turrets forward direction to the target to stop the barrel snapping back since the aim target is coming from my camera and the other two angles calculate the rear facing the same as the forward.

         float aimB = Vector3.Angle(aimTDir, -transform.forward);
 
         aimBAngle1 = Mathf.Asin(Vector3.Cross(aimTDir.normalized, -transform.forward).x) * Mathf.Rad2Deg;
         aimBAngle2 = Mathf.Asin(Vector3.Cross(aimTDir.normalized, -transform.forward).z) * Mathf.Rad2Deg;
 
 
         if(aimB <= 45.0f){
         if(aimBAngle1 <= 45.0f && aimBAngle1 >= -20.0f && aimBAngle2 <= 10.0f && aimBAngle2 >= -10.0f){
             Quaternion bRot = Quaternion.LookRotation(aimPoint - barrel.position);
             barrel.rotation = Quaternion.Slerp(barrel.rotation, bRot, Time.deltaTime * 5.0f);           
         }
         }

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
avatar image
0

Answer by Cherno · Feb 12, 2015 at 12:04 PM

Turret and barrel rotation gave me a huge headache as well. I finally got a satisfactory setup working, I don't understand parts of my own code as I copied it from another user :)

 public Transform turretTransform; //drag the turret GO here
 public Transform barrelTransform; //drag the barrel GO here
 public float turretSpeed = 45f;
 public float barrelSpeed = 45f;
 private Quaternion qGun;
 private Quaternion qGunStart;
 private float maxGunAngle;
 public float barrelRotMin = -10.0f;//play with this value to change how far the barrel can rotate downwards (actually an increase in rotation from 0 towards 10)
 public float barrelRotMax = 35.0f;//play with this value to change how far the barrel can rotate downwards (actually a decrease in rotation from 0 towards 335 (=-35))
 private Quaternion qTo;
 
 //call in Start()
 public void LoadTurretStats() {
 
         Quaternion qMin = new Quaternion();
         qMin.eulerAngles = new Vector3(barrelRotMin, 0, 0);
         Quaternion qMax = new Quaternion();
         qMax.eulerAngles = new Vector3(barrelRotMax, 0, 0);
         qGunStart = Quaternion.Slerp (qMin, qMax, -0.5f);//use a negative value or the min/max values will be reversed!
         maxGunAngle = Quaternion.Angle(qMin, qMax) / 2;
         gunStart = qGunStart.eulerAngles.x;
     }
 
 //Call CoRoutine from Update() while the vehicle/turret is active
 //pass it the hit.point from your raycast
 IEnumerator TurnTurret(Vector3 turretTargetPos ) {
 
         float distanceToPlane = Vector3.Dot(turretTransform.up, turretTargetPos - turretTransform.position);
         Vector3 planePoint = turretTargetPos - turretTransform.up * distanceToPlane;
         
         Quaternion qTurret = Quaternion.LookRotation(planePoint - turretTransform.position,turretTransform.up);
         turret.turretTransform.rotation = Quaternion.RotateTowards(turretTransform.rotation, qTurret, turretSpeed * Time.deltaTime);
 
         Vector3 v3 = new Vector3(0f, distanceToPlane, (planePoint - turretTransform.position).magnitude);
         turret.qGun = Quaternion.LookRotation(v3);
 
 
         barrelTransform.localRotation = Quaternion.RotateTowards(barrelTransform.localRotation, turret.qGun, barrelSpeed * Time.deltaTime);
         ClampRotation(barrelTransform, -maxGunAngle,  maxGunAngle,qGunStart.eulerAngles.x);
 
 
         yield return null;
     }
 
 //Used as a sub-function of te TurnTurret CoRoutine
 void ClampRotation(Transform tr, float minAngle, float maxAngle, float clampAroundAngle)     {
         //clampAroundAngle is the angle you want the clamp to originate from
         //For example a value of 90, with a min=-45 and max=45, will let the angle go 45 degrees away from 90
         
         //Adjust to make 0 be right side up
         clampAroundAngle += 180;
         
         //Get the angle of the z axis and rotate it up side down
         float x = tr.rotation.eulerAngles.x - clampAroundAngle;
         
         x = WrapAngle(x);
         
         //Move range to [-180, 180]
         x -= 180;
         
         //Clamp to desired range
         x = Mathf.Clamp(x, minAngle, maxAngle);
         
         //Move range back to [0, 360]
         x += 180;
         
         //Set the angle back to the transform and rotate it back to right side up
         tr.rotation = Quaternion.Euler(x + clampAroundAngle, tr.rotation.eulerAngles.y, tr.rotation.eulerAngles.z);
     }
Comment
Add comment · Show 2 · 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 DeadKenny · Feb 12, 2015 at 05:35 PM 0
Share

Thanks I will see what this can do.

I will reply if it does.

avatar image DeadKenny · Feb 13, 2015 at 01:08 PM 0
Share

Didn't work, but I solved it. Thanks for going trhough the effor to help though. $$anonymous$$y answer is below.

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

Add 90 Degrees to Transform.Rotation 2 Answers

Working on a mini map 0 Answers

Get slerp to work just as LookAt(,Vector3.right) does 1 Answer

Rotation Jumping values (0 to 180) 1 Answer

Problem in Euler angles 2 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