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 DrKillinger · Oct 18, 2013 at 09:18 PM · rotationanglelimitturret

Limiting Local Turret Rotation

Hi guys

There are a few similar questions out there, but none of them seem to work in my case.

Basically, I have a turret on a vehicle that tracks a target. It will always rotate towards the target:

 public int turretSpeed = 90;

 void FixedUpdate () {
         //if the vehicle has a target
         if(gameObject.transform.parent.GetComponent<Unit>().target)
         {
             Quaternion facing = Quaternion.LookRotation(gameObject.transform.parent.GetComponent<Unit>().target.transform.position - transform.position);
             transform.rotation = Quaternion.RotateTowards(transform.rotation, facing, turretSpeed * Time.fixedDeltaTime);
         }
 }

This works fine, but I'd like to make the turret unable to rotate more than 90 degrees on either side, but I've been unsuccessful thus far.

I tried placing this after the code:

 Vector3 angle = transform.localEulerAngles;
 angle.y = Mathf.Clamp(angle.y, -90.0f, 90.0f);
 transform.localEulerAngles = angle;

But this proved unpredictable, and the turret would spin 360 degrees after the parent object moved.

Can anyone offer some advice?

Comment
Add comment · Show 2
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 robertbu · Oct 19, 2013 at 04:47 AM 0
Share

I cannot fully visualize this problem. You have some sort of parent object. What kinds of rotations does it do and for what reason. Is it limited in any way. The script above goes on the child. I don't see any limits on the targeting code, so right now it can point this object in any direction to acquire the target. Does it point in arbitrary directions or is something limiting where it points.

Finally you talk about clamping the 'Y' rotation. What is the frame of reference for the angle? Are you 'just' clamping in relationship to the local forward?

avatar image DrKillinger · Oct 19, 2013 at 07:41 AM 0
Share

Basically, it's a tank in an RTS. I tell the vehicle to move around, and it does (using A*). On top of it, I have the child gameobject with the turret script included above.

As the code appears in the top block, it has no limits. I right click on a target, and the turret tracks the target, no matter what. What I want is to say something like "The turret can't twist more than 90 degrees left or right, 40 degrees up, and 10 degrees down", for example.

The solution I attempted was to check each update if the turret was twisted more than 90 degrees in local space, and set it to 90 so that it doesn't go further, but it didn't work.

Sorry if my question was vague.

0 Replies

· Add your reply
  • Sort: 

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

14 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

Related Questions

Limit local rotation 6 Answers

problems with quaternion rotations 4 Answers

Limit Object Angle 2 Answers

Need to limit the rotation angles on a turret 1 Answer

How do I limit the vertical rotation of a camera? 0 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