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 kievar1983 · Jan 03, 2012 at 08:41 PM · c#rotationgameobject

rotating an object only on z-axis help - c#

I'm attempting to adjust the barrel of a gun up and down (to show it's going fire a further distance)

I can get it to rotate just fine, but i only want it to rotate with in a certain radius otherwise you can just keep flipping it.

 if  (Input.GetKey(KeyCode.S) && gun.transform.rotation.z >= 350 || gun.transform.rotation.z <= 20))
 gun.transform.RotateAroundLocal(Vector3.back, (rotateRate * Time.deltaTime) * -1f);

this doesn't seem to work. I thought maybe the rotation would be in radians (that broke things) so i believe my math is messed up..

so this is what i want to do, have a gun that moves down when you press S and if it goes to 350 (after wrapping past 0,) it stops..

on the other side, i would like to do the opposite with W where if it goes from 350 (wrapping past 0) to 20 it stops at 20.

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 Winterblood · Jan 03, 2012 at 09:32 PM

The default rotation vector is interpreted as a quaternion, which is better for the maths but really hard to visualize. Try localEulerAngles instead, which is the XYZ rotation relative to the parent object:

 Vector3 rot = gun.transform.rotation.localEulerAngles;
 if  (Input.GetKey(KeyCode.S) && rot.z >= 350 || rot.z <= 20))
     rot.z -= rotateRate * Time.deltaTime;
 gun.transform.rotation.localEulerAngles = rot;

You may also need to handle wrapping if the Z exceeds 360 or drops below 0, because it won't do it for you.

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 kievar1983 · Jan 04, 2012 at 02:07 AM 0
Share

this kind of works, but it never stops spinning and the second i let go of s or W it just keeps spinning.

avatar image
0

Answer by kievar1983 · Jan 04, 2012 at 03:25 AM

i've changed how i am checking my code, i'm not using a second rotation to set it, cause it's not catching it, i also threw in some variables to check for wrapping. I also built in a boolean catch to try and stop it, some time it works some times i can get past it, some times it doesn't catch it at all...

this is my full update code for it.

 if(Input.GetKeyDown(KeyCode.W))
         {
             rotate = true;
         }
         if (Input.GetKeyDown(KeyCode.S))
         {
             rotate = true;
         }
         if (rotate)
         {
     
             if  (Input.GetKey(KeyCode.S) && (gun.transform.rotation.eulerAngles.z >= 350 || gun.transform.rotation.eulerAngles.z <= 20))
             {
                     if (wrapped == false && tooFar == true && Input.GetKey(KeyCode.S))
                     {
                         tooFar = false;
                     }
                     if (gun.transform.rotation.eulerAngles.z <= 0 && Input.GetKey(KeyCode.S))
                     {
                         wrapped = true;
                     }
                     if (gun.transform.rotation.eulerAngles.z <= 350 && wrapped == true && Input.GetKey(KeyCode.S))
                     {
                         tooFar = true;
                     }
                     else
                     {
                         tooFar = false;
                     }
                     
                     if (tooFar == false)
                     {
                         gun.transform.RotateAroundLocal(Vector3.forward, (rotateRate * Time.deltaTime) * -1f);
                     }
                     if (tooFar == true)
                     {
                         rotate = false;
                     }
             }
             else if  (Input.GetKey(KeyCode.W) && gun.transform.rotation.eulerAngles.z <= 350 || gun.transform.rotation.eulerAngles.z >= 20)
             {
                     if (gun.transform.rotation.eulerAngles.z >= 359 && Input.GetKey(KeyCode.W))
                     {
                         wrapped = false;
                     }
                     if gun.transform.rotation.eulerAngles.z >= 20 && Input.GetKey(KeyCode.W) && wrapped == false) 
                     {
                         tooFar = true;
                     }
                     else
                     {
                     tooFar = false;
                     }
                     if (tooFar == false)
                     {
                         gun.transform.RotateAroundLocal(Vector3.back, (rotateRate * Time.deltaTime) * -1f);
                     }
                     if (tooFar == true)
                     {
                         rotate = false;
                     }
             }
             else
             {
                 rotate = false;
             }
         }
     }

still could use some help on this, i'm not sure if i should use the second vector as i never caught anything with trying it.

thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Rotate GameObjects Regardless of List Size 2 Answers

Flip over an object (smooth transition) 3 Answers

C# GameObject Reverses Z Rotation 0 Answers

Using script's method from all of the gameobjects that has that script 2 Answers

Camera to follow the player in the form of radius 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