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 iamevn · Dec 26, 2011 at 11:07 AM · camerafollowzoomdolly

Camera dolly/zoom to keep object on screen

I've been trying to set up a camera for a 2d game that moves on the x axis to follow an object, which moves on the x and y axis, and zooms out to keep it on screen (currently by dollying back on the z) and then back in.

The zoom out is smooth and works fine, but the zoom back in is choppy. Here is how it currently is: [[example]][1] (please excuse the silly music and placeholder everything)

How can I make this more of a smooth movement? (relevant code below)

 var cam : Transform;
 var object : Transform;
 
 private var follow : boolean;
 
 private var camX : float;
 private var camY : float;
 private var camZ : float;
 private var objX : float;
 private var camZStart : float;
 private var screenPos : Vector3;
 private var bufferOut : float;
 private var bufferIn : float;
 var bufferRatio : float;
 var camMaxZ : float = 50;
 private var camZTarget : float;
 
 function Start()
 {
     camZStart = camera.transform.position.z;
     bufferOut = Screen.height/bufferRatio;
     bufferIn = Screen.height/bufferRatio;
     camZTarget = camZStart;
 }
 
 function Update()
 {
     if (Launch.launch)
     {
         WaitForObj();//waits for object before moving horizontally
     }
     
     camX = camera.transform.position.x;
     camY = camera.transform.position.y;
     camZ = camera.transform.position.z;
     
     
     
     objX = object.transform.position.x;
     
     screenPos = camera.WorldToScreenPoint(object.transform.position);
     
     if (Mathf.Abs(camZ) < camMaxZ){
         if (screenPos.y > Screen.height-bufferOut && object.rigidbody.velocity.y > 0){
             camZTarget = 1.02 * camZ;
         }
     }
     if (camZ < camZStart){
         if (screenPos.y < Screen.height-bufferIn && object.rigidbody.velocity.y <= 0){
             camZTarget = 0.9 * camZ;
         }
     }
 }
 
 function WaitForObj()
 {
     if (follow == false && objX >= camX){
         follow = true;
     }
 }
 
 
 function LateUpdate()
 {
     if (follow){
         transform.position.x = objX;
         transform.position.y = camY;
     }
     transform.position.z = camZTarget;
 }


[1]: http://html-bin.appspot.com/aghodG1sLWJpbnIMCxIEUGFnZRj1kAoM

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 Statement · Dec 26, 2011 at 01:46 PM

Maybe you could use a smoother function like Mathf.MoveTowards or Mathf.SmoothDamp?

 float speed = 1.0f; // Adjust this to proper speed
 transform.position.z = Mathf.MoveTowards(transform.position.z, 
                                          camZTarget, speed * Time.deltaTime);


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera following dice with FREE UNITY DICE PACK. help 1 Answer

ortographic camera smooth follow with zoom 1 Answer

Scene view camera zooming rather than dollying 1 Answer

Understanding Gun Scopes & Fov 1 Answer

Super Monkey Ball type Camera? 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