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 /
  • Help Room /
avatar image
0
Question by PoontangPlatoon · Mar 22, 2018 at 05:34 PM · transformpositionlerppublicpingpong

Lerp Confusion

Hi all,

I have this block of code: using UnityEngine; using System.Collections;

 public class EnemyMove : MonoBehaviour
 {
     public Transform startMarker;
     public Transform endMarker;
 
     void Update()
     {
         transform.position = Vector3.Lerp(startMarker.position, endMarker.position, Mathf.PingPong(Time.time, 1));
     }
 }

Basically, it lerps an object from a start to an end marker, then goes back at the same speed and repeats. However, I want to attach it to multiple objects that will move at specific speeds. At the moment, all objects reach the start/end markers at the same time.

How would I change the duration of the movement for each object, preferably as a public variable so that I can edit it from the Unity editor?

Thanks!

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

Answer by Scribe · Mar 22, 2018 at 09:25 PM

The important bit for 'speed' is the last argument of Lerp, firstly it is important to know that when the third value is 0 we are at our start position, and at 1 we are at the end position:

 Vector3.Lerp(positionA, positionB, 0) == positionA;
 Vector3.Lerp(positionA, positionB, 1) == positionB;

Currently you have the third arg set to Mathf.PingPong(Time.time, 1) which means:

  • at 0 seconds it equals 0

  • at 1 second it equals 1

  • at 2 seconds it equals 0

  • at 3 seconds it equals 1 and so on.

This means we go there and back again once every two seconds. If you set it to Mathf.PingPong(Time.time/2.0, 1) we would go there and back again once every 4 seconds, i.e. twice as slowly!

as @Hellium says in the comments, extracting that to a variable means you can control how many seconds it takes to travel:

 Mathf.PingPong(Time.time / duration, 1);
Comment
Add comment · 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 PoontangPlatoon · Mar 23, 2018 at 11:21 AM 0
Share

I did some playing around with this and the last number is how much of the path the object completes.

For example, if it is changed to 0.5 it will complete half the path, and if it is changed to 2 it tries to complete double the path length (but gets stopped by a wall, so stops for a while until it's time to move back).

avatar image Hellium PoontangPlatoon · Mar 23, 2018 at 11:50 AM 2
Share

You must call it as follow ins$$anonymous$$d :

 $$anonymous$$athf.PingPong(Time.time / duration, 1);

Where duration can be your public variable

avatar image Scribe Hellium · Mar 23, 2018 at 05:20 PM 1
Share

Oops! quite right, I've updated my answer, hope that's okay :)

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

161 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 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 not cooperating with me 0 Answers

gameobject position lerp 0 Answers

how to move the enemy in the current position of the player but only ones? 0 Answers

Camera isn't move position? Why my camera isn't change position? 0 Answers

I am trying to say if the Y of my player reaches below 0 change the players position to 0,1,0 it wont work thought and I tried using Vector3 but it says I cant use Vector3 like a method Please help! 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