• 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
Question by BilboStabbins · Nov 20, 2012 at 02:39 PM · rotationdirectionclamplimitlock

Lock rotation (range of motion) of auto-aiming bullet spawner

Hi, I have an auto-aiming bullet spawner attached to my Player and would like to limit its range of motion so that it cannot aim too far off to the left/right/up/down. I have come across a few techniques using Mathf.Clamp and Quaternions but cannot get t$$anonymous$$s to work as it should. I am looking for say a -30 to 30 degree limit for both the X and the Y rotation. The aiming is simply ac$$anonymous$$eved using a trigger ahead of the Player w$$anonymous$$ch detects any enemies entering and then aims at them if they are inside it.

My script simply takes in the direction of the enemy and then assigns the bullet spawner's direction to t$$anonymous$$s. It then uses LookRotation to look in the direction, thus:

 transform.rotation = Quaternion.LookRotation(bulletDirection);

Any help would be great. Thanks

Comment

People who like this

0 Show 0
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

Answer by Olgo · Nov 20, 2012 at 03:23 PM

You could use somet$$anonymous$$ng like t$$anonymous$$s...

             if(target){
                 Vector3 dir = (target.position - _myTransform.position).normalized;
                 float direction = Vector3.Dot(dir, transform.forward);
                 
                 dir = (target.position - _myTransform.position).normalized;
                 direction = Vector3.Dot(dir, transform.right);
                 
                 if(direction > rotLimit){
                     //turn aimer left
                 }
                 else if(direction < -rotLimit){
                     //turn aimer right                
                 }
                 else{
                     //do not$$anonymous$$ng                
                 }
             }
             else{
                 //do not$$anonymous$$ng
             }

Then copy the same t$$anonymous$$ng for Up and Down, maybe add a distance check if you want it to have a specific range. T$$anonymous$$s would keep the auto aimer tracking the target at the max of its rotation (might be good for a turret in a tower defense type game). Not sure if that's exactly what you want but it would be easily modified for other purposes.

Comment

People who like this

0 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 BilboStabbins · Jan 29, 2013 at 03:34 AM 0
Share

Hi Olgo, forgive the delay in replying, have been busy working on other stuff :)

Thanks for the script, it looks like what I'm after. However, I wonder if you can clear something up for me.

Can you explain what the _myTransform variable represents? I can see that 'target' would the enemy and that the transform (object script it attached to) would be the bullet spawner, so what might this be?

avatar image Olgo · Jan 29, 2013 at 03:12 PM 0
Share

_myTransform is the (cached) transform of the object this script is attached to. That way you can access _myTransform.position for use in distance calculation or for normalizing a vector between 2 points.

avatar image

Answer by Temo2Ginal · Apr 23, 2014 at 06:17 PM

I t$$anonymous$$nk t$$anonymous$$s will help. Click!

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Clamp rotation of a GameObject 1 Answer

Limiting RotateTowards on two axes 1 Answer

limit accelerometer controlled rotation 1 Answer

Controlling camera rotation limits. 1 Answer

Copy Rotation Direction only on Y Axis. 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