• 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
Question by Irsan · Feb 26, 2011 at 07:50 AM · camera-movementlerpsmoothtargets

Smooth camera movement between two targets.

Rather than, without a transition, switching from one camera to another one, I want the camera to move from the player to the target. Since I want to make it move smoothly (slow, than fast, then slowing down again), I assume this uses "lerp". Unfortunately I'm a complete newbie at this stuff. Help, please.

Also, the camera is currently in a first-person perspective with the player. It will be togglable to third-person later, but I could probably alter the script to my liking around then.

Comment
Fortyseven

People who like this

1 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by anomalous_underdog · Feb 26, 2011 at 08:54 AM

You'll have to use one camera only, that lerps between one point to another. You didn't give any code as to what first person view script you are using so I'm not sure how your code works, but generally you can store the Transform of the target, then make your camera lerp to the position of that target.

Here is some generic code to make a Vector3 lerp transition (C#). You'll want to put this in a script for your camera:

public float transitionDuration = 2.5f; public Transform target; IEnumerator Transition() { float t = 0.0f; Vector3 startingPos = transform.position; while (t < 1.0f) { t += Time.deltaTime * (Time.timeScale/transitionDuration);

     transform.position = Vector3.Lerp(startingPos, target.position, t);
     yield return 0;
 }

}

Assign a value to target by creating an empty GameObject, putting it at the place you want, then drag it to the target variable. You can initiate a transition by using:

StartCoroutine(Transition());

anywhere in the same file where the transition code is in.

Comment
Kalu
RFAronson
WNS-Studio
Irati
PWalsh23
MatasValiunas

People who like this

6 Show 4 · 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 Irsan · Feb 26, 2011 at 10:22 AM 0
Share

All I'm using for the camera is the default "MouseLook" script. Also, I'd like to stick with javascript, unless it happens to be remarkably similar to C#. Sorry.

avatar image anomalous_underdog · Feb 27, 2011 at 05:17 AM 0
Share

You can study the code then create the Javascript equivalent. I think in Javascript you don't need to put IEnumerator, for starters.

Learn how to program.

avatar image Kalu · Nov 08, 2011 at 10:01 PM 0
Share

Really good piece of code >anomalous. 10 minute to get what i needed:)

avatar image seansteezy · Aug 13, 2014 at 02:56 PM 0
Share

Works perfectly, thank you so much. I tried to change too much, messed it all up, then put this back like this and it fixed everything.

avatar image

Answer by seansteezy · Aug 13, 2014 at 03:16 PM

For anyone else that wants a more specialized lerp function, i added my rendition, totally based off of the great work by @anomalous_underdog. Thanks dude! This is used to move the camera to a specific point and the relative speed thing are the two bounds in which the camera can move between.

That way you can use it like this (where camPanDuration is either a variable or you can put in numbers on the fly, farLeft.position is where i want to lerp to and true just uses relative time). It's pretty specific to moving something between two points.

  public void resetCam()
     {
       StartCoroutine(LerpToPosition(camPanDuration, farLeft.position, true));    
     }
 
     IEnumerator LerpToPosition(float lerpSpeed, Vector3 newPosition, bool useRelativeSpeed = false)
     {    
         if (useRelativeSpeed)
         {
             float totalDistance = farRight.position.x - farLeft.position.x;
             float diff = transform.position.x - farLeft.position.x;
             float multiplier = diff / totalDistance;
             lerpSpeed *= multiplier;
         }
 
         float t = 0.0f;
         Vector3 startingPos = transform.position;
         while (t < 1.0f)
         {
             t += Time.deltaTime * (Time.timeScale / lerpSpeed);
 
             transform.position = Vector3.Lerp(startingPos, newPosition, t);
             yield return 0;
         }    
     }

Hope that helps someone!

Comment
tomatosource
Batz_

People who like this

2 Show 0 · 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

Answer by tistik · Feb 28, 2012 at 07:03 PM

I am implementing a similar behaviour at which I have added a reorientation of the camera towards the target. It works, however when the target object is behind the camera, the camera goes down in a weird move. I would like to prevent it from going too much towards the ground during the travelling (that is to say that the angle between the camera's forward and the ground should never be greater than 80°).

Does anyone have an idea on how to implement such a behaviour ?

Thanks in advance,

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Movement Script 1 Answer

Move A Camera Between Two Points Smoothly 1 Answer

Smoothing motion with vector3 lerp 3 Answers

interpolate a move variable 0 Answers

Smoothly stop rotating 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