• 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 TimBorquez · Aug 12, 2013 at 11:32 PM · rotationchildaxislookatlocalrotation

Local child rotation and transform.LookAt

ok so i know similar questions have been asked and answered a ton of times but i am still having trouble.

i have a gun that i am using transform.LookAt to look at the player, i want to attach the gun to an enemy, the problem is i want the gun to stay relatively forward facing on the enemy (so like the gun can turn 45 degrees left/right while the enemy still holds the gun forward)

i have tried:

 transform.LookAt(target.transform.position);
     if(transform.rotation.y > .45)
         {
             transform.rotation.y = .45;
         }else if(transform.rotation.y < -.45)
         {
             transform.rotation.y = -.45;
         }

it works stationary but it's the rotation in the world so once the enemy turns it's all ruined

So i tried euler angles:

 transform.LookAt(target.transform.position);
 if(transform.localEulerAngles.y > 45)
     {
         transform.localEulerAngles.y = 45;
     }
     if(transform.localEulerAngles.y < 315)
     {
         transform.localEulerAngles.y = 315;
     }

it works with one if statement at a time but i dont know how to apply both because looking completely straight is 360/0 degrees and i want to limit its left/right rotation at 45/315

i know the answer must be simple and im stupid but it's been a few days now and i dont know how to solve this, 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
Best Answer

Answer by robertbu · Aug 13, 2013 at 12:46 AM

Directly accessing 'eulerAngles' or 'localEulerAngles' can cause issues. Rotations are stored internally as Quaternions, and the eulerAngle you read back out may not be the one you put in. Also it is recommended to not assign axis independently. From the manual:

"Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations."

With all this said, it is possible what you are doing will work...especially if the x, and y values of the localEulerAngle remain near 0. So if what I'm about to suggest does not work, you will have to approach the problem differently.

As for your code, try this:

 if(transform.localEulerAngles.y > 45 && transform.localEulerAngles.y <= 180)
     {
        transform.localEulerAngles.y = 45;
     }
     else if(transform.localEulerAngles.y < 315 && transform.localEulerAngles.y > 180)
     {
        transform.localEulerAngles.y = 315;
     }
Comment
Add comment · Show 1 · 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 TimBorquez · Aug 13, 2013 at 01:17 AM 0
Share

thank you very much this is exactly i needed, i really dont know why i didnt think of that haha

if using euler can cause problems i might try to find a more foolproof way, thanks again

avatar image
0

Answer by IgorAherne · Aug 12, 2013 at 11:38 PM

Your question is hard for me to grasp, but I think what you need is to clamp the values using the Clamp() function

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 TimBorquez · Aug 12, 2013 at 11:51 PM 0
Share

yeah it's hard to explain... basically look at being used on an object but have the Y axis limited how much it can look left/right

ill look into clamp thanks

avatar image TimBorquez · Aug 13, 2013 at 12:20 AM 0
Share

ok clamp is awesome but i still have the same problem, im trying to limit the gun's(thats a child of the enemy) rotation and with euler angles i dont know how to put a limit at 45 and 315 degrees

heres a pic to show what i want if anyone can help alt text

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

16 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

Related Questions

LookAt() with X axis 1 Answer

[Please!] Noob need's help with planet gravity movement. 2 Answers

How can I get my enemy to rotate to look at my character in my 2D game?? 2 Answers

Rotate with joystick - LookAt axis flipping 1 Answer

Match rotation of gameobejcts in one (Y) axis 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