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 NoaB · Oct 09, 2013 at 08:52 PM · cameracamera-lookcamera-scrolling

How can I make a smooth camera transition over time with java script?

I'm doing a side scroller platform game and have two scripts for the camera. One makes it work as a side scroller camera, maintaining it's distance and following the character, and the other makes the camera go farther away from the character (so you can see more of the level) when entering a trigger, and back to it's original position when exiting the trigger:

 #pragma strict
 
 var Target : Transform;
 static var distanceFromPlayer = 10;
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function LateUpdate () {
     transform.position = Target.position + Vector3(distanceFromPlayer,0,0);
 }

Side scroller camera

 function Start(){
 
 }
 
 function OnTriggerStay(other : Collider){
 
   sidescrollerCameraScript.distanceFromPlayer = 18;
 }
 
 function OnTriggerExit(other : Collider){
 
   sidescrollerCameraScript.distanceFromPlayer = 10;
 
 }

Triggered camera switch:

Both work fine, but the switch of the camera looks too snappy, taking one frame to change positions. so I tried making it look smoother using the yield command like this:

 function OnTriggerStay(other : Collider){
 
   sidescrollerCaemeraScript.distanceFromPlayer = 11;
   yield WaitForSeconds(0.2);
   
   sidescrollerCaemraScript.distanceFromPlayer = 12;
   yield WaitForSeconds(0.2);
   
   sidescrollerCameraScript.distanceFromPlayer = 13;
   yield WaitForSeconds(0.2);
   
   sidescrollerCameraScript.distanceFromPlayer = 14;
   yield WaitForSeconds(0.2);
   
   sidescrollerCameraScript.distanceFromPlayer = 15;
   yield WaitForSeconds(0.2);
   
   sidescrollerCameraScript.distanceFromPlayer = 16;
   yield WaitForSeconds(0.2);
   
   sidescrollerCameraScript.distanceFromPlayer = 17;
   yield WaitForSeconds(0.2);
   
   sidescrollerCameraScript.distanceFromPlayer = 18;
   
 }

But this made unity confused and instead of following through it makes the camera go back and ford multiple times in a fraction of a second when inside the trigger, not what I intended.

So is there a way to affect the variable over time from point A all the way to point B so the camera transition looks smoother?

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 Cherno · Oct 09, 2013 at 11:27 PM

Just do it as you would make any other object move, with a while / for loop: var StartPosition : Vector3 = transform.position; var EndPosition : Vector3;

         var t : float = 0.0;
           
         while (t < 1.0) 
         {
             t += Time.deltaTime;
             transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
             yield;
         }

obviously you use your required distanceFromPlayer variables instead of Vector3s.

Comment
Add comment · Show 3 · 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 NoaB · Oct 10, 2013 at 12:42 AM 0
Share

so if I understand correctly, my code should look like this?:

 while (t < 1.0)
 {
       t += Time.deltaTime;
       sidescrolerCameraScript.distanceFromPlayer = sidescrolerCameraScript.distanceFromPlayer(10, 18, t);
 
 }

or am I wrong?

avatar image NoaB · Oct 10, 2013 at 01:16 AM 0
Share

ok no Im wrong, could you show me an example of how to work it with my distanceFromPlayer variable?

avatar image Cherno · Oct 13, 2013 at 02:39 AM 0
Share
 var $$anonymous$$axDistance : float = 20;
 var Player : GameObject;//drag your player object here
 var CameraSpeed : float = 0.5;
 
 function $$anonymous$$oveCamera()
 {
     while (Vector3.Distance(transform.position, Player.transform.position) < $$anonymous$$axDistance)
     {
         transform.Translate(-Vector3.forward * CameraSpeed);
         yield;
     }
 }

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

16 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

Related Questions

Need help for camera view like Air Strike 3d game ? 1 Answer

Enable the OrbitCam script how can I make it will continue from the current camera view and position ? 0 Answers

Camera gets stuck when cursor is locked 0 Answers

How to make the second camera rotate automatically? 1 Answer

Mouselook in space, without a 'down' or 'up' 1 Answer


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