Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by austingraham · Apr 03, 2017 at 06:48 AM · rotationinputanglesaimingcannon

[Solved - sort of] Can't find a way to lock cannon aiming between 2 angle values

So I have been browsing previous questions but haven't had any luck figuring out my specific problem. I have a cannon that I want the user to be able to vertically aim. I can get the cannon to respond to input but now I need to lock it within what I consider valid points.

The cannon is rotated on the x-axis. 0 is straight up and 90 would be pointing forward. So if I only want the player to be able to adjust the cannon between say 45 degrees and 90 degrees, the valid area would look like this: alt text

Ok. So that behaves as expected (watching the inspector as it rotates confirms this.) However, when I try to query what angle the cannon body is at, I get odd results. I currently am trying to get it by euler angles but I have tried pretty much everything I have been able to find or can think of to check the rotation but I always get a weird result. Here are the results logged when I try and check rotation in the code: (also please ignore the spelling errors in rotation) alt text

Here is the code snippet where I log its angle and do actual manipulation: (cannonBody is a gameObject refrence)

 print("X roatation: " + cannonBody.transform.eulerAngles.x.ToString());
 cannonBody.transform.Rotate(-Input.GetAxis ("Vertical") * 2,0.0f, 0.0f);

So, if I were to say check if the cannon is rotated less than 90 degrees I would get false results back. What is the best way of going about this?

Posting my findings as an answer.

valid-angles.jpg (111.6 kB)
actual.jpg (116.0 kB)
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
0

Answer by austingraham · Apr 04, 2017 at 02:02 AM

So I was able to get the behavior I expected after watching this video and adapting the code to suit my configuration.

Here is what I ended up doing:

 public float maxAimAngle;
 public float minAimAngle;
 private float currentRotation = 75.0f;

 ***
 Update calls input handler which calls handleArrowInput()
 ***
 
 private void handleArrowInput(){
     if(Input.GetAxis ("Vertical")!=0){
         currentRotation += -Input.GetAxis ("Vertical") * 2;
         currentRotation = Mathf.Clamp(currentRotation, minAimAngle, maxAimAngle);
         cannonBody.transform.eulerAngles = new Vector3(currentRotation,cannonBody.transform.eulerAngles.y, cannonBody.transform.eulerAngles.z);
     }
 }

The cannon is started rotated at 75 degrees on the x-axis. So, I just manipulated the variable, clamped it and then set the new rotation to the result.

If anyone does have an answer as to why when I was querying the eulerAngles it gave me odd results I'd love to here it. Also if you know of a better way of accomplishing this please feel free to let me know.

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

116 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Problem with projectile rotation - 2D 0 Answers

How to rotate an object around a player when pushing a button..? 0 Answers

Creating a simple character controller (Script provided) 1 Answer

Angle between objects taking in consideration their rotations? 0 Answers

using RayCast to aim at different planes 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