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
1
Question by simeonradivoev · Mar 17, 2012 at 12:11 AM · cameraplayerobjectzoomsmoothly

Zoom camera based on position between player an object

So I have an object, and I want the camera to zoom in on the player as he walks closer to the object. Also my camera has a variable for distance to player.

Here is my attempt (noob alert)

var zoomToBed = true; var player : Transform; var mainCamera : Transform; var zoomDistance = 10.0 ; private var camDefZoom ;

 function OnTriggerEnter (other : Collider){
     while(zoomToBed){
         yield;
         mainCamera.GetComponent(CameraScrolling).distance = camDefZoom - ((15 - 10)*(((this.transform.position.x / player.transform.position.x)*100)/100));
     }
 }

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 Kaze_Senshi · Mar 17, 2012 at 03:14 AM

I was thinking something like this that you made. You get the distance between the player and the wall, and use it as a variable between 0 and 1 multiplied by your camera distance. I recommend you to "interpolate" the zoom value instead of just change using the Lerp function, to make it more "smoothly". The code is something like ( i didn't test this one ):

 ...
 var MaxDistPlayerWall : float = 30.0; /* One value close from the maximum distance between the objects */
 var distPlayerWall : float = /*calculate distance*/
 var maximumZoom : float = 5.0; /*zzzooomm*/
 
 mainCamera.GetComponent(CameraScrolling).distance = Mathf.Lerp( mainCamera.GetComponent(CameraScrolling).distance, mainCamera.GetComponent(CameraScrolling).distance + ( maximumZoom * (distPlayerWall/MaxDistPlayerWall) )
 ...
 


Reference: http://unity3d.com/support/documentation/ScriptReference/Mathf.Lerp.html

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 simeonradivoev · Mar 17, 2012 at 04:46 AM

I did it ! You just need to put a object, in my case it was at a wall. Then you need to assign how far from the object the zoom will occur. You might add a if statement so that the zooming will stop when the player is at a specific position, if your object is in the middle of the scene. This is meant for a sidescroller, that's why i use only the x value.

PS. I don't know if the code can be more optimized, I would appreciate if someone can optimize it more for multiple uses in a level.

  var zoomBoundary = 15;
     var zoom = 13;
     var player : Transform;
     var mainCamera : Transform;
     var defZoom;
     
     function Start () {
     defZoom = mainCamera.GetComponent(CameraScrolling).distance;
     }
     
     function Update () {
         if (player.transform.position.x < zoomBoundary)
             mainCamera.GetComponent(CameraScrolling).distance = zoom +((defZoom - zoom)*(player.transform.position.x -this.transform.position.x) /(zoomBoundary - this.transform.position.x));
             
         else
            mainCamera.GetComponent(CameraScrolling).distance = defZoom;
     }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to rotate smoothly behind target? 0 Answers

Object disappears when zoom 0 Answers

putting an object's x coordinate to another object 1 Answer

Rotating camera around object up/down problem 0 Answers

How to Rotate Camera around GameObject (Player) 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