Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by W1zzel · Jul 10, 2020 at 06:49 PM · rotationquaternioneuleranglesquaternionseuler

Need help with storing Rotation in a Quaternion

Hi,

I have a mobile FPS Game where I use a joystick to rotate my Camera.

I have implemented an AimAssist but when it gets deactivated the rotation jumps back to where my last "camRotation" was because I am never updating its value while aimLock = true;

I have tried multiple things with quaternion.euler and so on but to be honest I am fishing in the dark with this Quaternion stuff.

So my question is: How do I update my camRotation so that when I use the joystick again, the camRotation is = the current rotation? Thanks!

 public class RotationClamp : MonoBehaviour
  {
      public float Offset;
      public float aimSpeed;
      public Transform target;
 
     [SerializeField]
     private Joystick joystick;
     [SerializeField]
     private Camera cam;
     private Quaternion camRotation;
     private bool aimLocked;
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         camRotation = transform.localRotation;
         _lookSpeed = lookSpeed;
     }
 
     // Update is called once per frame
     void Update()
     {
         RaycastHit hit;
   
         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, 100f))
         {
             if (hit.transform.CompareTag("Soldier"))
             {
                 target = hit.transform;
                 aimLocked = true;
             }
         }
        
 
         if (!aimLocked)
         {
             camRotation.x += joystick.Vertical * Time.deltaTime * _lookSpeed * verticalMultiplier * (-1);
             camRotation.y += joystick.Horizontal * Time.deltaTime * _lookSpeed;
         }
         camRotation.x = Mathf.Clamp(camRotation.x, -verticalAngle, verticalAngle);
         camRotation.y = Mathf.Clamp(camRotation.y, -horizontalAngle, horizontalAngle);
     }


     private void resetAimLock()
     {
         aimLocked = false;
         target = null;
     }
 
     private void FixedUpdate()
     {
         if (aimLocked)
         {
             Quaternion OriginalRot = transform.localRotation;
             transform.LookAt(target);
             Quaternion NewRot = transform.localRotation;
             transform.localRotation = OriginalRot;
             transform.localRotation = Quaternion.Lerp(transform.localRotation, NewRot, aimSpeed * Time.deltaTime);
             transform.localRotation *= Quaternion.Euler(Offset, 0, 0);
         }
         else
         {
             transform.localRotation = Quaternion.Euler(camRotation.x, camRotation.y, camRotation.z);
         }
     }
 }
 

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

Answer by UnityedWeStand · Jul 10, 2020 at 09:22 PM

Quick Solution


whenever you switch off aim assist, just convert the transform's current rotation into euler angles. camRotation = transform.rotation.eulerAngles

Recommended Solution


There is almost no reason to separately declare an XYZ rotation camRotation variable to store and modify the camera's rotation. Quaternions are so much easier to work with once you get the hang of them. For example, I would rewrite lines 43 and 44 to be
 if (!aimLocked)
          {
             transform.rotation = Quaternion.AngleAxis(joystick.Vertical * Time.deltaTime * _lookSpeed * verticalMultiplier * (-1), transform.right) * transform.rotation;
             transform.rotation = Quaternion.AngleAxis(joystick.Horizontal * Time.deltaTime * _lookSpeed, transform.up) * transform.rotation;         
          }

and then you don't have to deal with a separate camRotation variable and convert back and forth between euler rotations and Quaternions.

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 W1zzel · Jul 11, 2020 at 12:31 AM 0
Share

Yes, that works pretty good but now I also got a z rotation. How do I lock that axis?

Also: How would you clamp the rotation?

avatar image UnityedWeStand W1zzel · Jul 11, 2020 at 01:58 AM 0
Share

Try adding this after you set camRotation

 camRotation.x = $$anonymous$$athf.Clamp(camRotation.x, -verticalAngle, verticalAngle);
 camRotation.y = $$anonymous$$athf.Clamp(camRotation.y, -horizontalAngle, horizontalAngle);
 camRotation.z = 0;  


Again, I highly recommend using Quaternions directly, since converting back and forth from euler angles sometimes doesn't behave nicely

avatar image W1zzel UnityedWeStand · Jul 11, 2020 at 01:23 PM 0
Share

Ok but that would require me to again use the camRotation variable. Is there a way to clamp the rotation with the way you have shown in your first answer?

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

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

Related Questions

How to fix flipping Euler anlges? 0 Answers

Can't Accurately Read Rotations 0 Answers

Is there no way to get reliable angles from a rigidbody? 2 Answers

Fixing the rotation around a particular axis using Quaternions? 2 Answers

trouble with Quaternion rotation in 360 axis degree 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