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 phatlipp · Apr 30, 2014 at 09:26 PM · javascriptobject

How do I fix my platform-moving code?

I have written code to make a box move back and forth on the X axis, like a moving platform. When I run it, I get a message that my box is at the origin. (I wrote the error message, but it's obviously an error.) The box doesn't move either.

 #pragma strict
 //TODO: update to run on any axis
 
 private var middlePosition : Vector3;
 private var startPosition : Vector3;
 private var endPosition : Vector3;
 private var directionInput : String;
 
 //find move radius
 var moveRadius : float = 3.0;
 //wait time (in sec)
 var timeToWait : float = 1.5;
 var speed : float = 1.0;
 private var speedFrame;
 speedFrame = speed * Time.deltaTime;
 
 function Start ()
 {
     getStartingPosition();
     Debug.Log("Starting Position is: " + startPosition + ".");
 }
 
 function Update () 
 {
     findStartAndEnd("negative");
     LerpThatThing(startPosition, endPosition, speedFrame);
     WaitForSeconds(timeToWait);
     findStartAndEnd("zero");
     LerpThatThing(startPosition, endPosition, speedFrame);
     findStartAndEnd("positive");
     LerpThatThing(startPosition, endPosition, speedFrame);
     WaitForSeconds(timeToWait);
     findStartAndEnd("zero");
     LerpThatThing(startPosition, endPosition, speedFrame);
     
 
 }
 
 function getStartingPosition ()
 {
     middlePosition = transform.position;
 }
 
 function findStartAndEnd (directionInput)
 {
     startPosition = middlePosition;
     
     if (directionInput == "negative")
         {
         endPosition = middlePosition - (moveRadius * Vector3.right);
         }
         
     else if (directionInput == "positive")
         {
         endPosition = middlePosition + (moveRadius * Vector3.right);
         }
         
     else
         {
         endPosition = middlePosition;
         }
 
 }
 
 function LerpThatThing (startPosition, endPosition, speed)
 {
     transform.position = Vector3.Lerp(startPosition, endPosition, speed);
 }

What's wrong with this? I thought it was a scope error, but changing variables didn't fix it.

PS: Lerp is a funny word.

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 Maerig · May 01, 2014 at 06:59 AM

Check the documentation.

Lerp stands for Linear Interpolation. In your case, its third parameter represents how much the platform has moved between startPosition and endPosition, as a float between 0 and 1. Here is how you should use it :

 Vector3.Lerp(startPosition, endPosition, elapsedTime / totalDuration);

Where elapsedTime is a float which you increase by Time.deltaTime at each frame, and totalDuration the total duration of the movement (you can compute it from the speed : totalDuration = Vector3.Distance(startPosition, endPosition) / speed).

Also, this would probably fit in a coroutine.

Comment
Add comment · Show 1 · 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 phatlipp · May 01, 2014 at 11:34 AM 0
Share

So how would I move an object from a starting point to an ending point over a certain amount of time? Is Lerp the incorrect function to use?

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Cannot access sprite of a gameobject 1 Answer

Amnesia style 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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