• 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 AjelG · Aug 03, 2017 at 09:21 AM · camerainertia

Camera with inertia and zoom

Hello to everyone. The code bellow is a camera script with inertia and zoom. It works perfectly. However when the script is running i cant do other camera transforms like slerp. Plz share your thoughts and examples.

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour 
 {
 
     private Vector3 firstpoint;
     private Vector3 secondpoint;
     private float xAngle= 0.0f; 
     private float yAngle = 0.0f;
     private float xAngTemp  = 0.0f; 
     private float yAngTemp  = 0.0f;
     private float targetAngleX = 0.0f;
     private float currentAngleX =  0.0f;
     private float targetAngleY =  0.0f;
     private float currentAngleY =  0.0f;
     public float perspectiveZoomSpeed = 0.1f;
 
 
     void  Start()
     {
         currentAngleY =  0.0f;
         currentAngleX =  0.0f;
         this.transform.rotation = Quaternion.Euler(currentAngleY, currentAngleX, 0.0f);
     }
 
     void Update() 
     {
         float xPos=Input.GetAxis("Mouse X");
         float yPos=Input.GetAxis("Mouse Y");
         if (Input.touchCount > 0) {
             if (Input.touchCount == 1) {
             
                 xPos = Input.touches [0].deltaPosition.x;
                 yPos = Input.touches [0].deltaPosition.y;
 
                 if (Input.GetTouch (0).phase == TouchPhase.Began) {
                     firstpoint = Input.GetTouch (0).position;
                     xAngTemp = xAngle;
                     yAngTemp = yAngle;
                 }
                 if (Input.GetTouch (0).phase == TouchPhase.Moved) {
                     secondpoint = Input.GetTouch (0).position;
                     yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;
                     targetAngleY += -yPos * 0.1f;
                     if (targetAngleY < currentAngleY) {
                         while (currentAngleY - targetAngleY > 360)
                             targetAngleY += 360;
                     } else if (targetAngleY > currentAngleY) {
                         while (targetAngleY - currentAngleY > 360)
                             targetAngleY -= 360;
                     }
                     xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
                     targetAngleX += xPos * 0.1f;
                     if (targetAngleX < currentAngleX) {
                         while (currentAngleX - targetAngleX > 360)
                             targetAngleX += 360;
                     } else if (targetAngleX > currentAngleX) {
                         while (targetAngleX - currentAngleX > 360)
                             targetAngleX -= 360;
                     }
                 }
             
             } else if (Input.touchCount == 2) {
                 Zooming ();
             }
         }
             currentAngleX += Time.deltaTime * ((targetAngleX - currentAngleX) * 3);
             if (Mathf.Abs (currentAngleX - targetAngleX) < 1)
                 targetAngleX = currentAngleX;
             currentAngleY += Time.deltaTime * ((targetAngleY - currentAngleY) * 3);
             if (Mathf.Abs (currentAngleY - targetAngleY) < 1)
                 targetAngleY = currentAngleY;
             transform.rotation = Quaternion.Euler (currentAngleY, currentAngleX, 0.0f);
     }
 
     void Zooming()
     {
         Touch touchZero = Input.GetTouch (0);
         Touch touchOne = Input.GetTouch (1);
         if (Input.GetTouch (0).phase == TouchPhase.Moved && Input.GetTouch (1).phase == TouchPhase.Moved) 
         {
             Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
             Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
             float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
             float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
             float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
             Camera.main.fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
             Camera.main.fieldOfView = Mathf.Clamp (Camera.main.fieldOfView, 40.0f, 65.0f);
         }
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

102 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

Related Questions

Is there an easy way to make my camera lag behind the vehicle? 1 Answer

How to make camera position relative to a specific target. 1 Answer

Touch Controlled Camera Inertia 1 Answer

Rotating gameobject after mouselook 0 Answers

Strange issue, movement direction has been reversed when X axis is negative 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