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 /
avatar image
0
Question by Turz1 · Dec 18, 2019 at 03:05 PM · rotationclamped rotation

How to clamp rotation in degrees in relation to another GameObject

So I am building a tank game in unity. As you know in tanks your turrets vertical movement is minimal and I have it clamped between 5 degrees down and 10 degrees up. This works fine on flat surface. My script takes my turrets current global rotation but I would like it to clamp the rotation in relation to my tanks main body. If I am in a hill the turret still wants to face up 10 degrees at max and the barrel goes trough my tanks body and the whole scene goes nuts. I will provide some pictures about the problem.alt text

In the first picture everything is fine and I can look down 5- and up 10 degrees.

alt text

In the second picture you can see the problem. I can not look any higher than this because the turret is in its highest position (10 degrees) but not in relation to the tank. All help is appreciated! I will also post the looking script here.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class KameranLiikkuminen : MonoBehaviour
 {
     [SerializeField] private string mouseXInputName, mouseYInputName;
     [SerializeField] private float mouseSensitivity;
 
     [SerializeField] private Transform playerBody;
 
     private float xAxisClamp;
 
     private void Awake()
     {
         LockCursor();
         xAxisClamp = 0.0f;
     }
 
     private void LockCursor()
     {
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     private void Update()
     {
         CameraRotation();
     }
 
     private void CameraRotation()
     {
         float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
         float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;
         
 
         xAxisClamp -= mouseY;
 
         //Kameran kääntymis rajoitus vertikaalisesti
         if(xAxisClamp > 5.0f)
         {
             xAxisClamp = 5.0f;
             mouseY = 0.0f;
             ClampXaxisRotationToValue(5.0f);
         }
         else if (xAxisClamp < -10.0f)
         {
             xAxisClamp = -10.0f;
             mouseY = 0.0f;
             ClampXaxisRotationToValue(-10.0f);
         }
 
         transform.Rotate(Vector3.right * -mouseY);
         transform.Rotate(Vector3.up * mouseX);
 
         ClampZaxisRotationToValue(0.0f);
     }
 
     private void ClampXaxisRotationToValue(float value)
     {
         Vector3 eulerRotation = transform.eulerAngles;
         eulerRotation.x = value;
         transform.eulerAngles = eulerRotation;
     }
 
     private void ClampZaxisRotationToValue(float value)
     {
         Vector3 eulerRotation = transform.eulerAngles;
         eulerRotation.z = value;
         transform.eulerAngles = eulerRotation;
     }
 }

toka.png (98.7 kB)
eka.png (98.3 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 unity_ek98vnTRplGj8Q · Dec 18, 2019 at 05:31 PM

If the turret is a child of the tank object (I assume it is) then you should clamp transform.localRotation.eulerAngles.x to what you want, maybe something like this:

 if(transform.localRotation.eulerAngles.x > 5){
     transform.localRotation = Quaterion.Euler(new Vector3(5, transform.localRotation.eulerAngles.y, transform.localRotation.eulerAngles.z));
 }


Comment
Add comment · Show 7 · 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 Turz1 · Dec 18, 2019 at 06:41 PM 0
Share

How would you implement that bit of code to my existing code? I am still a bit unfamiliar with C# so could you help me to do that. Thanks for the quick answer!

avatar image unity_ek98vnTRplGj8Q Turz1 · Dec 18, 2019 at 08:03 PM 0
Share

@Turz1 Try this out, I haven't tested it so let me know if it works or not. If not, we can tweak it to do exactly what you want. A couple things to note: I went ahead and changed around the structure of your code a bit if that's O$$anonymous$$. I also deviated a bit from the answer I gave to you earlier to more closely follow best practices for creating rotations (at least according to my understanding).

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class $$anonymous$$ameranLiikku$$anonymous$$en : $$anonymous$$onoBehaviour
  {
      [SerializeField] private string mouseXInputName, mouseYInputName;
      [SerializeField] private float mouseSensitivity;
  
      [SerializeField] private Transform playerBody;
  
      private float xAxisClamp, yAxisClamp;
  
      private void Awake()
      {
          LockCursor();
          xAxisClamp = 0.0f;
      }
  
      private void LockCursor()
      {
          Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
      }
  
      private void Update()
      {
          CameraRotation();
      }
  
      private void CameraRotation()
      {
         //Get mouse inputs
         float mouseX = Input.GetAxis(mouseXInputName) * mouseSensitivity * Time.deltaTime;
         float mouseY = Input.GetAxis(mouseYInputName) * mouseSensitivity * Time.deltaTime;
         
         //Increment your x and y rotation values
         xAxisClamp -= mouseY;
         yAxisClamp += mouseX;
  
         //Clamp x value
         if(xAxisClamp > 5.0f) xAxisClamp = 5.0f;
         else if (xAxisClamp < -10.0f) xAxisClamp = -10.0f;
 
         //$$anonymous$$ake a rotation about the LOCAL x axis
         Quaternion rotation = Quaternion.AngleAxis(xAxisClamp, transform.right);
         //$$anonymous$$ake a rotation about the PARENT'S local y axis 
         rotation = rotation * Quaternion.AngleAxis(yAxisClamp, transform.parent.up);   
         //Set this rotation as the local rotation
         transform.localRotation = rotation;   
         
      }
 
  }

The reason that I am not using transform.Rotate() here is because this function is good for adding a delta rotation. That makes it harder to clamp, however, because you want to clamp your overall rotation, not your delta rotation. Ins$$anonymous$$d, I make an entirely new Quaternion every frame so that I can set the overall rotation which I can easily clamp.

avatar image Turz1 unity_ek98vnTRplGj8Q · Dec 19, 2019 at 09:24 AM 0
Share

Thanks a lot! This seems to work better but I think you have to also lock z axis because now the turret can turn around z axis and it get all twisted. Otherwise this seems to have fixed my problem. How can I completely lock the z axis in this script? I tried to mess around with it but I only got it to work with turrets local z axis again :( @unity_ek98vnTRplGj8Q

Show more comments
Show more comments

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

152 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

Related Questions

How to use mathf.clamp? 2 Answers

How to Clamp a Quaternion Camera Rotation? 0 Answers

Clamp rotation of object - Door 1 Answer

Clamped rotation of turret problem 0 Answers

How to clamp rotation between negative and positive value 2 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