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 wuannetraam · Nov 22, 2019 at 10:00 AM · scripting problemcamera rotate

Camera Rotation on Touch. Clamp rotation.

I'm not a programmer and have been searching a lot but I can't find out how to do this. Hope you can help me with this small problem. I have this script which is working great for me. Only thing I want to add to this is to clamp the rotation of the camera to for example 40 degrees max.

 using UnityEngine;
 using System.Collections;
 
 [AddComponentMenu("Camera-Control/Touch Look")]
 public class TouchLook : MonoBehaviour
 {
 
     public float sensitivityX = 5.0f;
     public float sensitivityY = 5.0f;
 
     public bool invertX = false;
     public bool invertY = false;
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.touches.Length > 0)
         {
             if (Input.touches[0].phase == TouchPhase.Moved)
             {
                 Vector2 delta = Input.touches[0].deltaPosition;
                 float rotationZ = delta.x * sensitivityX * Time.deltaTime;
                 rotationZ = invertX ? rotationZ : rotationZ * -1;
                 float rotationX = delta.y * sensitivityY * Time.deltaTime;
                 rotationX = invertY ? rotationX : rotationX * -1;
 
                 transform.localEulerAngles += new Vector3(rotationX, rotationZ, 0);
             }
         }
     }
 }

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
0

Answer by Ermiq · Nov 22, 2019 at 01:00 PM

You could check the angle between some pivot transform forward vector and the forward vector that the camera will have when new rotation is applied. If the angle is more than 40 degrees, don't apply the rotation.

But you can't use camera transform as a pivot obviously, because its forward vector is changed by the rotation. So, for example, you'll need to use player character transform as a pivot, but the character should not be rotated by the camera.

By the way, in new Vector3(rotationX, rotationZ, 0) the parameter rotationZ should be called rotationY, because vector parameters are x, y, z.

 // before you apply the rotation, check the angle first
 // player.transform is a pivot transform that will represent the initial straight forward vector.
 float angle = GetAngle(player.transform, transform, new Vector3(rotationX, rotationY, 0));
 if (angle < 40f)
     transform.localEulerAngles += new Vector3(rotationX, rotationY, 0);
 
 private float GetAngle(Transform pivot, Transform camera, Vector3 rotationToBeApplied) {
     // get the direction vector that the camera will have upon the rotation application
     // to get that direction you'll need to get the resulting rotation as a quaternion. We'll take current camera rotation, rotate it (the rotation, not the camera) x degrees around X axis, and y degrees around Y axis.
     Quaternion q = camera.rotation * Quaternion.AngleAxis(rotationToBeApplied.x, Vector3.right) * Quaternion.AngleAxis(rotationToBeApplied.y, Vector3.up);
     // now we've got the rotation, convert it into a direction vector
     Vector3 cameraResultingDirection = q * Vector3.forward;
     // get the angle between the pivot and this new camera vector
     float angle = Vector3.Angle(cameraResultingDirection, pivot.forward);
     return angle;
 }
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 lgarczyn · Nov 23, 2019 at 04:55 AM

I would advise against reading and storing your current orientation from a quaternion. The constant conversion between quaternion and Euler angle is often bad, even if it shouldn't cause problems here.

My advisor would be storing a rotationY and a rotationX float inside TouchLook, that store the current rotation.

Use inputs to increment them to their max values, and only then convert them to a quaternion using Quaternion.Eulers or two Quaternion.AngleAxis

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

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

Setting the FPS Camera to a certain position 0 Answers

smooth camera rotation problem 1 Answer

Problems with Gyroscope VR Car Game 0 Answers

Clamp a Vector above minimum angle from another Vector? 0 Answers

View Matrix Camera rotation problem 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