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 TRON-dll · Jul 27, 2012 at 03:33 AM · c#rotationturret

Need to limit the rotation angles on a turret

So I have a turret on a fixed surface. I need to keep it from moving past certain angles.

See this image:[1]

[1]: /storage/temp/2184-rotationexample.jpg

My rotation degrees are probably incorrect in the context of Unity, though they are just there so I can better explain my issue.

I need the turret (the black box in the image) to only be able to rotate so that it's facing anywhere between 90 and -90 degrees (in the context of the image). I have a feeling that using a Mathf.Clamp function may be the answer here, but the function itself has kind of confused me, so some further explanation on how it works would be very much appreciated.

I prefer answers to be in C#, though I can probably translate from Javascript.

EDIT: I should also probably add in that the turret starts off facing towards 0 degrees (again, in the context of the image).

EDIT 2: Here is my code for rotating the base of the turret:

 //for rotating the turret base
 transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * rotateSpeed, 0) * Time.deltaTime);

And my code for tilting the gun up and down:

 gun.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * rotateSpeed, 0, 0) * Time.deltaTime);
rotationexample.jpg (13.6 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
Best Answer

Answer by supernat · Jul 27, 2012 at 07:05 AM

Do you have code that performs the rotation, or are you utilizing a third party utility? Somewhere, there's code to allow the rotation similar to the following:

 // If left mouse down, rotate left, else rotate right, for example
 if (left)
   transform.localEulerAngles = transform.localEulerAngles + new Vector3(0.0f, Time.deltaTime*rotateRate, 0.0f);
 else if (right)
   transform.localEulerAngles = transform.localEulerAngles + new Vector3(0.0f, -Time.deltaTime*rotateRate, 0.0f);

In this example, you are rotating the turret left or right. To clamp it, you could instead:

   Vector3 angle = transform.localEulerAngles;
   angle.y = Mathf.Clamp(angle.y + Time.deltaTime*rotateRate, -90.0f, 90.0f);
   transform.localEulerAngles = angle;

The clamp function simply enforces that the final value is between the min and max (the min is the second parameter and max is the third).

rotateRate here is how many degrees per second you allow the turret to rotate. You only rotate around the angle.y as the Y axis is your UP axis (in this example).

I use the localEulerAngles because if the turret is sitting on a ship that is swaying left/right such that the World coordinate Y axis is not aligned with the turret's Y axis, the turret will rotate around the Y axis of the parent (ship) instead. If you never rotate your ship around the X or Z axes, you could use the eulerAngles instead of localEulerAngles. Hope that makes sense.

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 TRON-dll · Jul 27, 2012 at 05:42 PM 1
Share

I actually do have some code to do the rotation. I'll stick it in the question after I finish posting this. But for the time being, here it is:

//for rotating the turret base transform.Rotate(new Vector3(0, Input.GetAxis("$$anonymous$$ouse X") rotateSpeed, 0) Time.deltaTime);

I should add that my turret is split into two parts, a base and the gun itself. The base will only turn left and right and the gun will only tilt up and down. What I've done is I've parented the turret base to the ship and the gun to the turret base, so the line of parenting is as follows:

playerShip --> turretBase --> gun

Additionally, this is my code for tilting the gun up and down: gun.transform.Rotate(new Vector3(Input.GetAxis("$$anonymous$$ouse Y") rotateSpeed, 0, 0) Time.deltaTime);

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

Play Sound when turret rotates 1 Answer

Multiple Cars not working 1 Answer

Limiting Local Turret Rotation 0 Answers

Turret Continuing to Max Rotation - Following Mouse Cursor 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