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 PiMuRho · Jul 15, 2012 at 08:52 AM · rotateclampturret

Turrets acting oddly when clamped

I've got a setup where I have a block that spawns 4 turrets - 2 on top, and 2 on the bottom. I wanted to clamp them so that they could only rotate 180 degrees laterally (90 left, 90 right) and 90 degrees vertically (from straight ahead to vertically up, relative to their position)

Clamping them horizontally worked okay. However, when clamping them vertically, whenever the turrets target something out of their movement range (i.e. less than 0 degrees, more than 90) they switch rapidly between their clamped values, so that they're bouncing up and down. How on earth do I get them to behave like they do horizontally?

Note on the turret setup - they have two bones for movement. One is the base (horizontal) and one is the barrel (vertical) each with their own relatively simple script. I'll paste the barrel one here, as that's the one with the issue. The base script is the same, except it's all on the y axis.

 var Target:Transform = null;
 var SwivelSpeed = 32.0;
 var localRotation : Vector3 ; //these 3 variables are here so I could check 
 var clampedRotation : float ; //the values in the inspector
 var targetAngle : float ;
 
 
 function Update () 
 {
 
 if(Target)
    
     {
     var localTarget: Vector3 =     transform.InverseTransformDirection(Target.transform.position - transform.position);
     targetAngle = Mathf.Atan2(localTarget.z, localTarget.y) * Mathf.Rad2Deg;
     var targetAngle2 = targetAngle - 90;
     var adjustedAngle = Mathf.Lerp(0, targetAngle2, Time.deltaTime * SwivelSpeed);
     transform.Rotate(Vector3.right, adjustedAngle ); //rotate the turret
     
      localRotation  = transform.localEulerAngles ; //get the local rotation values
     
     clampedRotation  = Mathf.Clamp(localRotation.x, 0, 90); //clamp to the values we want
     //force the turret rotation to be clamped
  transform.localEulerAngles.x = clampedRotation;
  transform.localEulerAngles.y = 0 ;
  transform.localEulerAngles.z = 0 ;
     }


Also, the fire control for the turret comes from another script in the hierarchy. How can I set it so that a turret that is unable to aim at the target can't fire? (I could use a raycast on each turret, but I'm after the cheapest method possible - I intend to have lots of turrets.

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

1 Reply

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

Answer by PiMuRho · Jul 18, 2012 at 02:30 PM

Okay, here's how I fixed it (both issues in one)

 if(Target)
 
     {
     var localTarget: Vector3 = transform.InverseTransformDirection(Target.transform.position - transform.position);
     var targetAngle : float = Mathf.Atan2(localTarget.z, localTarget.y) * Mathf.Rad2Deg;
     var targetAngle2 : float = targetAngle - 90;
     var adjustedAngle = Mathf.LerpAngle(0, targetAngle2, Time.deltaTime * SwivelSpeed);
        
     var localRotation : Vector3 = transform.localEulerAngles ;
     var expectedNewAngle : float = transform.localEulerAngles.x + adjustedAngle;
       
     if (expectedNewAngle > 1 && expectedNewAngle < 90) 
     {
     transform.Rotate(Vector3.right, adjustedAngle );
     allowFire() ;
     }
     
     if (expectedNewAngle < 1 || expectedNewAngle > 90)
     {
     ceaseFire();
     }  
   }
 }

ceaseFire() just calls a function on the projectile generator and stops it from firing. So now my turrets have nicely clamped movement, and they don't fire when they can't "see" the player.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate Turret where touched (top down view) 1 Answer

How to make the turret on your tank turn 0 Answers

Mini Gun Turret 1 Answer

Clamping a wrapping rotation. 6 Answers

Turret rotation clamp. 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