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
1
Question by simon · Sep 14, 2010 at 11:46 PM · lerpmovesmoothtargetpoint

How to move an object from point A to point B with one key press

I'm sure this must be amazingly simple but I can't figure how to move one object from one point to another by using a single keystroke.

At the moment I'm trying get a platform object to smoothly lift from point A to point B when I press the spacebar.

My current code looks like this:

var target : GameObject;

private var liftSpeed = 10;

function Update () {

if(Input.GetButtonDown("Jump"))

 {

 transform.position = Vector3.Lerp(transform.position, target.transform.position, Time.deltaTime * liftSpeed)

 }

}

This however, only moves the platform by the specified fraction, and I have to repeatedly press space until the platform reaches its target. How do I get it to continually move upwards until it reaches the target by just pressing the spacebar once?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by skovacs1 · Sep 15, 2010 at 02:27 PM

Your code is only running when you hit the space bar because it will only perform the transformation inside the if statement when the space bar is hit.

If you wanted it when you hold the space bar, you would use Input.GetButton("Jump") instead.

If you want it just to start the transform and run on its own, you could set a flag or something like this:

var target : GameObject; //destination var liftSpeed : float = 10; //speed (it will complete the motion in 1/speed seconds) private var moving : boolean = false; //flag private var weight : float = 0; //amount moved private var startPosition : Vector3; //Where we start;

function Update () { if(target) { if(Input.GetButtonDown("Jump")) { //just pressed Jump startPosition = transform.position; //Set the start moving = true; //set flag } if(transform.position != target.transform.position) moving = false; //reset flag if(moving) //check flag weight += Time.deltaTime * liftSpeed; //amount transform.position = Vector3.Lerp(startPosition, target.transform.position, weight) } } }

The reason I changed the code to store a start position and weight is to provide a linear interpolation. By lerping from the current position (which you are constantly changing) and a target, you will move by the weight amount of the remaining distance (which is constantly decreasing), meaning that the movement will start fast and will slow down as it approaches the destination which it may never actually reach. There's nothing wrong with this form of interpolation and if that's what you wanted, by all means change that part back. If you wanted it to smooth in and out, I'd recommend using something like Mathf.SmoothStep(weight) in the Lerp.

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 simon · Sep 15, 2010 at 08:04 PM 0
Share

Thanks that's very helpful! Works like a dream :)

avatar image
0

Answer by cengizhangokben · May 28, 2013 at 08:00 AM

was not there an error? It must be the condition like that;

 if(transform.position == target.transform.position) 
             devam = false; //reset flag

:)

Comment
Add comment · 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
0

Answer by bbyford · Jan 31, 2015 at 11:19 PM

Quick moving an object script via transform in C# with timetaken:

 bool somethingMoved
 somethingMoved = moveSomething(yourObject, endObject); // change room bool

 

 // move GameObject to GameObject marker
     float timeTaken;
 
     public bool moveSomething(GameObject start, GameObject end){ // return bool when finished moving
 
         if(start.transform.position == end.transform.position){
             return false;
         }else{
             timeTaken += (float) move(start, end);
             return true;
         }
     }
     public float move(GameObject start, GameObject end){
         start.transform.position = Vector3.Lerp(start.transform.position, end.transform.position, Time.deltaTime*camSpeed);
         return Time.deltaTime*camSpeed;
     }
Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Move to a clicked point? 1 Answer

How Can I Make The Parkour Moves Move Smoothly Instead Of Instantly Teleport? 1 Answer

Lerp has some bumping effect 1 Answer

Smoothly moving object along 1 axis 1 Answer

How can I find the velocity on a rigidbody which moves using Lerp? 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