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 J_LS · Jun 03, 2014 at 07:22 AM · cameralerppan

Pan-Lerp a camera

Hi, I am new here, and I hope that anyone help me. I am trying to pan a camera with a scipt that I am placing in the main camera. But the camera just jumps a little between the 2 points and it doesn't arrive to the newPosition. I will copy my script, thank you:

 void Update () {    

     if (Input.GetMouseButtonDown (0)) {
         Vector3 newPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         Vector3 currentPosition = transform.position;

         transform.position = Vector3.Lerp (currentPosition,  newPosition, Time.deltaTime);
     }
 }
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 Zodiarc · Jun 03, 2014 at 07:32 AM

I assume you're pressing the left mouse button only once. That would explain this behavior.

The way the code works is like this: You press the LMB and immediatelly let go. What happens is (assuming your press was exactly 1 frame long), the if clause will be entered only once and never again. Consider that everything within the Update function is called once every frame.

Te effect is, that the camera moves a bit every time you press the LMB.

I see two solutions to this:

  1. You change the GetMouseButtonDown to GetMouseButton and hold the LMB longer or

  2. You control it with a bool variable like this

    boolean isMoving = false;

    Vector3 newPosition;

    void Update () {

      if (Input.GetMouseButtonDown (0)) {
            isMoving = true;
            newPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         }
         if (isMoving){
            transform.position = Vector3.Lerp (transform.position,  newPosition, Time.deltaTime);
         }
         // To avoid the camera lock on one position
         if(transform.position == newPosition){
             isMoving = false;
         }
     }
    
    

Hope this helps

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

Rotate, pan and zoom camera lerp touch 0 Answers

Changing camera position to shoot through scope on gun 1 Answer

Lerping orthographic camera size jitters 1 Answer

Smooth camera shift, Lerp? SmoothShift? 2 Answers

Vector3.SmoothDamp() - smoothTime unit 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