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 Xitech_ · Dec 30, 2013 at 09:34 PM · vector3.slerp

Vector3.lerp teleporting instead of lerp?

I have some clouds in my game wich i want to move left and right. I have the following code:

     private Vector3 startPos;
     private Vector3 endPos;
     void Start () {
         startPos = transform.position;
         endPos = new Vector3(startPos.x + 10f,startPos.y,startPos.z);
     }
     
     // Update is called once per frame
     void Update () {
         transform.position =  Vector3.Lerp(startPos,endPos,1f);
     
     }
 }

For some reason the cloud is not lerping. But teleporting to the destination instead Before I run the game the cloud is positioned here: alt text

after its positioned here: alt text

I know a speed of 1 could be fast but any number isnt working at all. I tried from 0.1-1 What am I doing wrong?

Cheers

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

Answer by sparkzbarca · Dec 30, 2013 at 09:45 PM

hehe, 1 is as fast as you can possibly get

i'll try to explain lerp to you

lerp has 3 components

a start point for the line

an end point for th line

and a third number which is 0 to 1

this third number can be viewed as this

"given a line with start point X and end point Y, give me the position Z percent distance along that line"

so a Z of 0 is the start, a Z of 1 is the end and a Z of .5 is the dead middle of the line

a speed of even .1 actaully would only take 10 frames to complete which is 1/6th of a second. In this case we actually need a special value.

This value is the time since the last frame, its referenced by typing time.deltatime

the proper way to use lerp in update is this

 cloud.transform.position =   vector3.lerp(cloud.transform.position, finalposition, speed * time.deltatime);


good luck!

Comment
Add comment · Show 6 · 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 Spinnernicholas · Dec 30, 2013 at 09:56 PM 0
Share

Good Answer!

avatar image Xitech_ · Dec 31, 2013 at 06:36 PM 0
Share

Thank you for your reply, I've already tried adding time.deltatime. Still no good result(I used the unity video as a reference). Somehow the cloud is moving from 5.607-5.509 ish. I've recorded it with Screenr. Please have a look: Screenr video EDIT: I think it has something to do with it being "in the camera" so the positioning is wrong. $$anonymous$$aybe add the position with position local

avatar image sparkzbarca · Dec 31, 2013 at 09:13 PM 0
Share

startpos must update

it must be

void Start() { Startpos = transform.position; }

Void Update() { Startpos = transform.position;

could.transform.postion = vector3.lerp(start,end,speed * time.deltatime)

if you what to do it that way

but

just get rid of start pos and feed lerp transform.position

the thing is if you read my explanation

right now you have a fixed line for lerp

it has start and end fixed and it just keeps fidning the same point.

what you need to do is find a point a small distance along, go to that point and now lerp from this new point with this small line thats why your start position must keep changing It has to be a variable not a constant or you'll keep getting the same point, thats why you move the first half second, it works but then it breaks because you dont update from that new position.

so

GET RID OF START POS

and change it in update to

transform.pos = Vector3.lerp( transform.position, EndPos, speed * time.deltatime);

the key is that transform.position will take care of updating itself so it'll keep your start pos updating properly.

please mark as answered and have a nice day!

avatar image Spinnernicholas · Jan 01, 2014 at 01:28 AM 0
Share

Alright here we go:

 public double speed;
 public GameObject cloud;
 //..................
 void Update()
 {
   cloud.transform.position = vector3.lerp(cloud.transform.position, finalposition, speed * time.deltatime);
 }
avatar image Xitech_ · Jan 05, 2014 at 12:15 PM 0
Share

I am not getting it. You use tranform.position. But the transform of the cloud is changing so it will keep on moving.

Show more comments
avatar image
1

Answer by Spinnernicholas · Dec 31, 2013 at 07:20 PM

You could accumulate the last argument:

 double lerpProgress = 0d;
 Vector3 start;
 //...........
 void Awake()
 {
   start = transform.position;
 }
 //...........
 void Update()
 {
   lerpProgress += speed * time.deltatime;
   cloud.transform.position = vector3.lerp(start, finalposition, lerpProgress);
   if(lerpProgress >= 1)
   {
     //Cloud Reached Destination, Do something....
     lerpProgress = 0;
   }
 }
Comment
Add comment · Show 9 · 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 sparkzbarca · Dec 31, 2013 at 09:09 PM 0
Share

THIS IS WRONG check my comment.

avatar image Spinnernicholas · Dec 31, 2013 at 09:20 PM 0
Share

This is separate from your answer because he isn't getting it.

avatar image sparkzbarca · Dec 31, 2013 at 09:22 PM 0
Share

well he got it, he just didn't realize he needed to update I guess technically your solution might work but its quite ugly :P

avatar image Spinnernicholas · Dec 31, 2013 at 09:27 PM 0
Share

lol, if I were to do it personally, I would probably wrap it in a enumerator function that returns true when done:

 if(lerpToPosition(startPosition, endPosition, totalTime))
 {
   //done.....
 }
avatar image Spinnernicholas · Dec 31, 2013 at 09:29 PM 0
Share

$$anonymous$$aybe even extend Transform:

 if(transform.lerpTo(position, time))
 {
   //done.....
 }
Show more comments

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

rotating road gameobject towards mouse position 1 Answer

Alien Swarm Control with Mecanim 0 Answers

C# to Jacascript 1 Answer

Vector3.Slerp - is there a way to force an arc? 0 Answers

Radius and angle of Vector3.Slerp motion arc 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