Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by pantakkun · Apr 19, 2016 at 04:16 PM · rotationgameobjectbooleangetkey

Rotation with limits not working?

So I'm trying to get a tank to aim its' gun up and down using the "q" and "e" key, the buttons work on their own but I want there to be a limit to the degrees on the y axis to how far down or how far up the gun can aim. the bool for up and down is to check whether or not i am able to rotate the GameObject up or down. Aiming up works well and is true as long as it's under 79 degrees, but for some reason aiming down never works and is off the entire time..i know that below 0 degrees just goes off from 360 degrees so i set it to no less than 10 degrees..is there something i'm missing, or is my code a bit flawed in the way it calls the actual rotation it's currently in?

there is a public gameobject that is just the sphere (or empty gameobject) that rotates the child cube that is the actual gun on the tank so that the sphere is the only thing that rotates changing the rotation of the cube along with it. The default position i set it to on the y Rotation is set at 40 degrees so that when i start the game, i can make sure that both functions should be set to true.

public GameObject Trajectory;

 public bool Up = true;
 public bool Down = true;

 public int rotationSpeed = 30;
 
 // Update is called once per frame
 void Update () {

     if (Trajectory.transform.rotation.eulerAngles.y > 79) {
         Up = false;
     } else {
         Up = true;
     }

     if (Trajectory.transform.rotation.eulerAngles.y < 10) {
         Down = false;
     } else {
         Down = true;
     }




     if (Up == true) {
         //AimUp
         if (Input.GetKey(KeyCode.E)){
             Trajectory.transform.Rotate(Vector3.up * rotationSpeed * Time.deltaTime);
         }
     }

     if (Down == true) {
         //AimDown
         if (Input.GetKeyDown(KeyCode.Q)){
             Trajectory.transform.Rotate(-Vector3.up * rotationSpeed * Time.deltaTime);
         }
     }

     Debug.Log (Trajectory.transform.rotation.eulerAngles);
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ryanless · Apr 19, 2016 at 06:47 PM

Just remove these bool functions completly and use Mathf.Clamp ( check API if there are question to it)

code could look like:

 //min max in Degree
 float minAngle;
 float maxAngle;

 //aim up
 //aim down
         
 //after the input u clamp
 Vector3 tAngles = Trajectory.transform.rotation.eulerAngles;
 tAngles.y = Mathf.Clamp (tAngles.y, minAngle, maxAngle);
 Trajectory.transform.rotation.eulerAngles = tAngles;
     

these makes sure the angle stays always inside (minAngle, maxAngle)

Comment
Add comment · Show 3 · 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 pantakkun · Apr 19, 2016 at 08:21 PM 0
Share

for that, i get an error on the last line:

Trajectory.transform.rotation.eulerAngles = tAngles;

for the following:

"Cannot modify the return value of 'UnityEngine.Transform.rotation' because it is not a variable"

avatar image Ryanless pantakkun · Apr 19, 2016 at 09:30 PM 1
Share

oh right replace last line with:

Trajectory.transform.eulerAngles = tAngles;

or

Trajectory.transform.localEulerAngles = tAngles;

that should work. Not sure if it should be local or world so best you try out on your actual cannon.

avatar image pantakkun Ryanless · Apr 21, 2016 at 10:20 AM 0
Share

Thank you so much!! I super appreciate it! I'm new to program$$anonymous$$g in Unity's C# if that wasn't obvious..still, I'll always be learning not any bit slower thanks to you!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I set a toggle to reverse the direction of a rotation? 0 Answers

Need to do multiple boolean check for completing level 3 Answers

Game object rotate with the camera on y axis 0 Answers

Clamp without Quaternion? 0 Answers

Locking facing direction or changing it after following the mouse position 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