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 Bentoon · Mar 19, 2014 at 05:43 AM · cameravector3lerpfirst-person-controller

Making Camera Lerp back to original position

Hello All,

I am Lerping a camera away from its FPC, exploring a bit and then I want to move it back to it's Original FPC

Here's my Code:

Works now: Zoom In:

     var target: Transform;
         var camPos: Vector3;
     var endPos: Vector3;
     var time : float;
 
     function OnMouseDown () {
     camPos = Vector3( Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z);
     endPos = Vector3( target.transform.position.x, target.transform.position.y, target.transform.position.z);
 
 
     
     var i = 0.0;
     var rate = 1.0/time;
     while (i < 1.0) {
         i += Time.deltaTime * rate;
         Camera.main.transform.position = Vector3.Lerp(camPos, endPos, i);
         yield; 
         
         }
 }



Zoom Out:

     var camPos: Vector3;
     var endPos: Vector3;
     var time : float;
 
     function OnMouseDown () {
     endPos = Vector3( Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z);
     
     var i = 0.0;
     var rate = 1.0/time;
     while (i < 1.0) {
         i += Time.deltaTime * rate;
         Camera.main.transform.position = Vector3.Lerp( endPos, camPos, i);
         yield; 
         }
 }        


I was hard coding Vector3's to get it near it's original position (& where the FPC is still) But throughout the gameplay The Cam & FPC became more and more separated which leads to problems Thanks

~ be

Comment
Add comment · Show 2
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 ShadoX · Mar 19, 2014 at 08:05 AM 0
Share

Don't think that something like that should happen unless your not returning the camera back to its original position...

also, ins$$anonymous$$d of

 camPos = Vector3( Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z);

why not just do

 camPos = Camera.main.transform.position;

?

That won't fix the problem, but looks nicer as the result is the same.. as for the problem..

any chance that you are moving the object while zoo$$anonymous$$g? as in.. you move the character while the camera is zoo$$anonymous$$g in/out so by the time the camera returns to "camPos" the character isn't actually at that spot anymore..

avatar image Bentoon · Mar 19, 2014 at 06:31 PM 0
Share

Thanks ShadoX

There is no character, just just the Camera & the First Person Controller Pill. When I Lerp the Camera it leaves the FPC to do so and I just want to get it back to the FPC at the end.

Something like (although not proper js) :

 function on$$anonymous$$ouseDown () {
 Camera.main.transform.Position = FirstPersonConto;;er.transform.position
 } 


Thanks

~be

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bentoon · Mar 19, 2014 at 08:50 PM

Hello All,

Here's what I found to work that both puts the Camera Back to the FPController and also lerps it more specifically to where you click initially:

Zooom In script:

     var target: Transform;
     var controller : GameObject;
     var camPos: Vector3; 
     var endPos: Vector3;
     var time : float;
 
 
            function Update () {
         var controller : CharacterController = GetComponent(CharacterController);
         }
         
     function OnMouseDown () {
     camPos = Vector3( controller.transform.position.x, controller.transform.position.y, controller.transform.position.z);;
     endPos = Vector3( target.transform.position.x, target.transform.position.y, target.transform.position.z);
 
     
     var i = 0.0;
     var rate = 1.0/time;
     while (i < 1.0) {
         i += Time.deltaTime * rate;
         Camera.main.transform.position = Vector3.Lerp(camPos, endPos, i);
         yield; 
         
         }
 }
 


THEN Zoom Out Script :

         var target: Transform;
         var controller : GameObject;
         var camPos: Vector3; 
         var endPos: Vector3;
         var time : float;
     
     
                function Update () {
             var controller : CharacterController = GetComponent(CharacterController);
             }
             
 
     function OnMouseDown () {
     endPos = Vector3( Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z);
     camPos = Vector3( controller.transform.position.x, controller.transform.position.y, controller.transform.position.z);;
 
     var i = 0.0;
     var rate = 1.0/time;
     while (i < 1.0) {
         i += Time.deltaTime * rate;
         Camera.main.transform.position = Vector3.Lerp( endPos, camPos, i);
         yield; 
         }
         
     }
     
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

21 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

Related Questions

How to smooth my camera (Vector3.Lerp) 0 Answers

(Vector3.Lerp) Camera doesn't reach the specified Destination! 1 Answer

Zooming camera using move z position. 0 Answers

Camera Lerp from A to B, and back 2 Answers

Need to flatten a Vector3 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