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 Nick4 · Feb 03, 2014 at 03:30 PM · lerpmovinggameobject

How to switch 2 objects' positions gradually?

 GameObject o1, o2;
 Vector3 o1Position, o2Position;
 bool access = false;
 float speed = 0.50f;
 
 void Update()
 {
    if ( /* it's necessary to switch these objects */ )
    {
       o1Position = o1.transform.position;
       o2Position = o2.transform.position;
       access = true;
    }
 
    if(access)
    {
       o1.transform.position = Vector3.Lerp(o1Position, o2Position, speed);
       o2.transform.position = Vector3.Lerp(o2Position, o1Position, speed);
    
       if(o1.transform.position == o2Position)
       {
          access = false;
       }
 
    }
 }

I try Lerp as you can see above but that only moves my objects half way to their destinations and they end up sharing the same positions. Is there another way to switch their positions slowly or how can I fix one? I played around with speed variable but it didn't help. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Feb 03, 2014 at 03:43 PM

If you don't want eased movement at the end, do:

  o1.transform.position = Vector3.MoveTowards(o1.transform.position, o2Position, speed * Time.deltaTime);
  o2.transform.position = Vector3.MoveTowards(o2.transform.position, o1Position, speed * Time.deltaTime);

'speed' will be units per second for movement.

Comment
Add comment · Show 5 · 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 Nick4 · Feb 03, 2014 at 03:52 PM 0
Share

No, that's not what I was asking. Neither o1 nor o2 reaches their destination. For example : if o2Position is (2,0,0) and o1 is at (1,0,0), o1 stops at (1.5, 0, 0).

avatar image robertbu · Feb 03, 2014 at 04:17 PM 0
Share

Yes I know what is happening. The problem is that your 'speed' parameter is set to 0.5f. That means that your two objects will immediately jump to the midpoint between the two positions. $$anonymous$$aking the change I recommended will fix the problem. Alternately you could implement a timer. Lerp()'s third parameter ranges from 0.0 to 1.0. You would then have as your third parameter 'timer/totalTime'.

avatar image Nick4 · Feb 03, 2014 at 04:51 PM 0
Share

I'm sorry but that didn't change a thing. Whatever the speed value is, they're not going more than halfway.

avatar image robertbu · Feb 03, 2014 at 05:03 PM 0
Share

I'd need to see your updated code to figure out what is wrong, but the two lines I posted are right. Here is a class based on your code above along with my two lines. It works fine.

 using UnityEngine;
 using System.Collections;
 
 public class Bug25d : $$anonymous$$onoBehaviour
 {
     public GameObject o1;
     public GameObject o2;
     Vector3 o1Position, o2Position;
     bool access = false;
     float speed = 0.50f;
     
     void Update() {
         if (!access && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
             o1Position = o1.transform.position;
             o2Position = o2.transform.position;
             access = true;
         }
         
         if (access) {
             o1.transform.position = Vector3.$$anonymous$$oveTowards(o1.transform.position, o2Position, speed * Time.deltaTime);
             o2.transform.position = Vector3.$$anonymous$$oveTowards(o2.transform.position, o1Position, speed * Time.deltaTime);
             
             if(o1.transform.position == o2Position && o2.transform.position == o1Position) {
                 access = false;
             }
         }
     }
 }

I did not see how you initialized 01 and 02. I initialized them through drag and drop. The two objects switch places when hte space bar is pressed.

avatar image Nick4 · Feb 03, 2014 at 05:17 PM 0
Share

Thanks, you've done your part. Issue with my script must be something else.

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

18 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

Related Questions

Moving Maincamera slowly to another position 0 Answers

rigidbody.velocity vs lerp efficiency 0 Answers

Moving an object constantly between two points with a time delay inbetween 2 Answers

lerp from a to moving position 0 Answers

Lerp / SmoothDamp complete before t=1 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