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 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 this 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 achieved using a trigger ahead of the Player which 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 this. It then uses LookRotation to look in the direction, thus:

 transform.rotation = Quaternion.LookRotation(bulletDirection);

Any help would be great. Thanks

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

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

You could use something like this...

             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 nothing                
                 }
             }
             else{
                 //do nothing
             }

Then copy the same thing for Up and Down, maybe add a distance check if you want it to have a specific range. This 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
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 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
0

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

I think this will help. Click!

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

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

limit accelerometer controlled rotation 1 Answer

Controlling camera rotation limits. 1 Answer

Copy Rotation Direction only on Y Axis. 1 Answer

Limit Rotations with Min and Max on Mobile 1 Answer

c# problem with limiting rotation (with rigidbody) 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