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 grossmeister21 · Sep 06, 2021 at 07:26 PM · rotationrotate objectlimitverticaltransform.rotatearound

How to limit the rotation of an object

I have a code that allows me to rotate my tank tower and its barrel using UI Joystick. The controls part is working, the problem is that I can't limit the rotation of my barrel (I need to lock it on 15 degrees). I've tried literally everything that my newbie hands can, but nothing works.

The closest I could get to my goal is the thing you see in the code (I commented it). It works, kinda. When I start a game, the barrel's X axis locks on -15 degrees and when I try to rotate the barrel up, as it reaches 0 degrees it changes to -15 degrees back. Please help! alt text

 using UnityEngine;
 
 public class TurretJoystick : MonoBehaviour
 {
     public float rotationYSpeed = 35f;
     public float rotationXSpeed = 35f;
     public VariableJoystick gunJoystick;
     
     public GameObject Gun; //tank barrel
 
     void Update()
     {
         //rotate turret (everything works because i don't need to lock the rotation)
         float turretRotation = gunJoystick.Horizontal * rotationYSpeed;
         turretRotation *= Time.deltaTime;
         transform.Rotate(0f, turretRotation, 0f);
 
         //rotate gun
         float gunRotation = gunJoystick.Vertical * rotationXSpeed;
         gunRotation *= Time.deltaTime;
         Gun.transform.Rotate(gunRotation, 0f, 0f);

         //this is the part where i tried to lock the rotation
         float minRotation = -15;
         float maxRotation = 15;
         Vector3 currentRotation = Gun.transform.localRotation.eulerAngles;
         currentRotation.x = Mathf.Clamp(currentRotation.x, minRotation, maxRotation);
         Gun.transform.localRotation = Quaternion.Euler(currentRotation);    
     }
 }


2021-09-06-222119.png (169.8 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Pangamini · Sep 08, 2021 at 12:59 AM

The problem with BobyStar's solutioni is that he keeps reacquiring euler angles from the rotation. This will cause the angles to 'snap around' sometimes. For example, the same rotation can be described by many different values. -90 degrees is the same rotation as 270 degrees. This messes with your clamping. Instead, try to keep the euler angles in a local variable (Vector3 currentRotation in your case), and don't set it from Gun.transform. That way, your gun rotation will always be represented by eulerAngles, so no 'snapping around' happens (since they are just floats in a vector).

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

Answer by BobyStar · Sep 06, 2021 at 08:17 PM

Instead of rotating the transform, you should change currentRotation.x by the gunRotation then limit the rotation. Lastly set the transform's local rotation to the new currentRotation:

  // rotates currentRotation
  Vector3 currentRotation = Gun.transform.localEulerAngles; // get local euler angles
  float gunRotation = gunJoystick.Vertical * rotationXSpeed;
  gunRotation *= Time.deltaTime;
  currentRotation.x += gunRotation; // adds gunRotation to the x axis of the currentRotation


  // limits currentRotation.x and applies it to the transform
  float minRotation = -15;
  float maxRotation = 15;
  currentRotation.x = Mathf.Clamp(currentRotation.x, minRotation, maxRotation);
  Gun.transform.localEulerAngles = currentRotation; // you can just use transform.localEulerAngles
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 grossmeister21 · Sep 07, 2021 at 02:48 PM 0
Share

unfortunately, it didn't work for me. it only restricts the downward movement, and when I try to raise the muzzle up, it resets to -15 again

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

170 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 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

limit rotation to 90 degree on one axis 3 Answers

How can I set a limit to my object rotation???(Here's the code) 2 Answers

Strange rotation pattern. 0 Answers

Resetting transform's original position and rotation after using transform.rotateAround 1 Answer

How can I rotate an object without moving it up or down? 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