• 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 mike29willems · Mar 05, 2019 at 01:06 PM · lerpsmoothfollow

Vector3.Lerp and MoveTowards not smoothing (simple script)

public Transform target; public float smoot$$anonymous$$ng = 5f; Vector3 offset; Vector3 targetCamPos;

 void Start() {
     offset = transform.position - target.position;
  }

 void FixedUpdate() 
 { 
 targetCamPos = target.position + offset;                                                           transform.position = Vector3.Lerp(transform.position, targetCamPos, smoot$$anonymous$$ng); 
 }

I have copied t$$anonymous$$s code from Unity tutorial. But if I put t$$anonymous$$s code on my camera and attach the transform of my object(that I wanna follow) the camera fly's away in one direction that is based on the opposite position of the object. I have also tried to change update/fixedupdate, Time.deltaTime and so on. I never could see the smoot$$anonymous$$ng change (when changing the value). Someone told me that it is a percentage but even then change the value from 0-1 had no impact (the camera would just follow or fly away) but no smoot$$anonymous$$ng. Iam using MacBook Pro and I have the latest version of Unity. I can make a short video so u can see everyt$$anonymous$$ng? Thanks in advance!

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
1
Best Answer

Answer by TreyH · Mar 05, 2019 at 01:37 PM

Differences between a position A and some other position B are usually given as (B-A), not the other way around (note that yours is A-B). (B-A) gives the vector required for A to reach B, so t$$anonymous$$s is why your situation is flying away instead of going towards your target.

Since you can t$$anonymous$$nk of the t value (your smoot$$anonymous$$ng) of a Lerp as how "close" the result will be to the two Vectors your provide, in your case it's returning a constant value. That offset value will always be some stagnant vector, but your targetCamPos is also being calculated as the current position plus that.

The code you posted would make more sense if you changed how your target position was being calculated:

 targetCamPos = target.position + offset;                                                           
 transform.position = Vector3.Lerp(transform.position, targetCamPos, smoot$$anonymous$$ng); 


T$$anonymous$$s would preserve the original offset between your object and its target. You can then change your smoot$$anonymous$$ng value to be somet$$anonymous$$ng between 0 and 1, as you mentioned, with slow / fast values being close to 0 / 1, respectively.

Comment
Add comment · Show 2 · 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 mike29willems · Mar 05, 2019 at 02:56 PM 0
Share
avatar image TreyH mike29willems · Mar 05, 2019 at 03:02 PM 0
Share
avatar image
0

Answer by mike29willems · Mar 05, 2019 at 02:37 PM

Thankyou for ur time. I see that I made a mistake copying the last part. So I have now changed (B-A) and target.position + offset but the camera goes then trough the target in a straight line. If I change the Lerp to MoveTowards the camera goes to the target but is not keeping the offset distance and ones he reach the target he goes the same as lerp trough the ground and never turn back. Do you know how I can let the camera follow the target so that it travels always to the offset (with smoot$$anonymous$$ng)?

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 TreyH · Mar 05, 2019 at 02:52 PM 1
Share
avatar image TreyH TreyH · Mar 05, 2019 at 02:55 PM 1
Share
avatar image mike29willems TreyH · Mar 06, 2019 at 01:34 PM 1
Share

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

102 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 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 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 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 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

Vector3.Lerp is not reacting, pls help (simple script) 2 Answers

Smooth animation scrubbing with the mouse 3 Answers

How can I get a game object to follow something smoothly without using lerp. 0 Answers

why target switching of smooth camera not very smooth ? 0 Answers

Mathf.Lerp not working as expected 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