Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mike29willems · Mar 05, 2019 at 01:06 PM · lerpsmoothfollow

Vector3.Lerp and MoveTowards not smoothing (simple script)

public Transform target; public float smoothing = 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, smoothing); 
 }

I have copied this code from Unity tutorial. But if I put this 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 smoothing 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 smoothing. Iam using MacBook Pro and I have the latest version of Unity. I can make a short video so u can see everything? 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 this is why your situation is flying away instead of going towards your target.

Since you can think of the t value (your smoothing) 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, smoothing); 


This would preserve the original offset between your object and its target. You can then change your smoothing value to be something 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

I was getting the result I wanted when I put offset = transform.position - target.position + vectorDistanceCameraFromPlayer; in fixed update and assign the public Vector vectorDistanceCameraFromPlayer to lets say (1, 1, 1). Do u know Why?

avatar image TreyH mike29willems · Mar 05, 2019 at 03:02 PM 0
Share

Adding a constant to the offset shouldn't change anything here as your offset is itself constant and its value is just being used to keep a distance from something, so adding that (1,1,1) probably just prevented you from overshooting.

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 smoothing)?

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

Then it sounds like something is weird in your scene, is one of those objects the parent / child of the other? If I create an empty scene with:

 using UnityEngine;
 
 public class LerpFollow : $$anonymous$$onoBehaviour {
     public Transform target;
     private Vector3 offset;
 
     void Start () {
         offset = transform.position - target.position;
     }
     void FixedUpdate () {
         transform.position = Vector3.Lerp (transform.position, target.position + offset, 0.1f);
     }
 }


Sitting on a cube with the target assigned to another cube, I get:

alt text

With the black cube here being the target and the white cube maintaining its initial offset.

avatar image TreyH TreyH · Mar 05, 2019 at 02:55 PM 1
Share

Even if you did it the other way around, with target.position - transform.position as your offset, the white block would just be sitting on the other side.

avatar image mike29willems TreyH · Mar 06, 2019 at 01:34 PM 1
Share

Thank you! It finally works :) The mistake I made was that my camera previously flew away (bad coding) so when he did it again I thought that he was flying endlessly away (same as before), but when I checked the transform this time of the camera and saw that it was not changing anymore, I realized that it was because he had reached his offset. In my setup I couldn't tell by looking at the camera if he was still moving or not. So even if ur mistakes are looking the same while having other code check if its still true. Thank you for ur time and help!

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

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

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

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

Problem with slerp 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