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 BudyDubby · May 21, 2015 at 03:56 AM · cameralerpzoomorthographic

How to zoom in smoothly with orthographic camera?

Hello, i currently making a camera system for Human anatomy like rotating object, zoom in when i clicked certain object, etc. Basically, i want my camera move like Human Anatomy Atlas.

what i already achieved is:

  1. Camera Rotation around target

  2. Double click on object to focus camera on it

but here the problem, i want to camera to focus & zoom smoothly to double clicked object. i already tried vector3.lerp but i don't understand it very well (sorry im still noob at this).

here my code :

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     public Transform target;
     public float zoomDistance = 0.4f;
     public float distance = 5.0f;
     public float xSpeed = 120.0f;
     public float ySpeed = 120.0f;
     
     public float yMinLimit = -20f;
     public float yMaxLimit = 80f;
     
     public float distanceMin = .5f;
     public float distanceMax = 15f;
     
     public float smoothTime = 2f;
     
     float rotationYAxis = 0.0f;
     float rotationXAxis = 0.0f;
     
     float velocityX = 0.0f;
     float velocityY = 0.0f;
 
     // double click variable
     float lastClick = 0;
     float catchTime = .25f;
 
     // Vector3.Lerp Variable
     public float speedLerp = 1.0f;
 
     private Transform startMarker;
     private Transform endMarker;
     private float startTime;
     private float journeyLength;
 
     // Use this for initialization
     void Start()
     {
         Rigidbody rgdbody = GetComponent<Rigidbody> ();
 
         Vector3 angles = transform.eulerAngles;
         rotationYAxis = angles.y;
         rotationXAxis = angles.x;
 
         // Make the rigid body not change rotation
         if (rgdbody)
         {
             rgdbody.freezeRotation = true;
         }
 
         startTime = Time.time;
     }
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
 
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
 
             // Vector3.Lerp
             startMarker = transform;
             endMarker = target;
             journeyLength = Vector3.Distance (startMarker.position, endMarker.position);
             // end Vector3.Lerp
 
             // double click check
             if (Time.time - lastClick < catchTime) { // if double click
                 if (Physics.Raycast (ray, out hit)) { // if click on object
                     target = hit.transform; // camera will rotation around new target
                     distance = zoomDistance; // camera will zoom clicked object
 
                     // Vector3.Lerp
                     float distCovered = (Time.time - startTime) * speedLerp;
                     float fracJourney = distCovered / journeyLength;
                     transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
                     // end Vector.Lerp
 
                 } else {
                     distance = 5f; // if clicked on blank space, it will zoom out
                 }
                 print ("its double click");
 
             } else {
                 print ("its normal click");
             }
             lastClick = Time.time;
         }
     }
 
 
     // Camera Rotation / Position / Zoom
     void LateUpdate() {
         if (target) {
             if (Input.GetMouseButton(0)) {
                 velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
                 velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
             }
             
             rotationYAxis += velocityX;
             rotationXAxis -= velocityY;
             
             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
             
 //            Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
             Quaternion rotation = toRotation;
 
             distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 1, distanceMin, distanceMax);
             Camera.main.orthographicSize = distance; // zoom with orthographic camera
 
 //            RaycastHit hit;
 //            if (Physics.Linecast(target.position, transform.position, out hit))
 //            {
 //                distance -= hit.distance;
 //            }
 
             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
             Vector3 position = rotation * negDistance + target.position;
             
             transform.rotation = rotation;
             transform.position = position;
             
             velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
             velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
         }
         
     }
 
     public static float ClampAngle(float angle, float min, float max) {
         if (angle < -360F)
             angle += 360F;
         if (angle > 360F)
             angle -= 360F;
         return Mathf.Clamp(angle, min, max);
     }
 }
 

Comment
Add comment · Show 1
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 BudyDubby · May 22, 2015 at 02:03 AM 0
Share

sorry, i've solved this matter... so there's no need to answers this question again, but i should say thank you for answering

sorry for grammar

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Creepgin · May 21, 2015 at 09:30 AM

When the cam is in ortho mode, changing its position won't achieve zooming. You'll need to change the fieldOfView field instead.

For Vector3.Lerp, you should do something like this instead:

transform.position = Vector3.Lerp(transform.position, destinationPosition, Time.deltaTime * speed);

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

20 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

Related Questions

Zooming camera using move z position. 0 Answers

How to make a smash bros-like camera 3 Answers

Dynamic Orthagraphic Camera Zoom 1 Answer

How to zoom camera in x amount of seconds 1 Answer

zoom in with orthografic camera with focus on the a specific point 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