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 AMK_Hydra · Dec 25, 2014 at 06:50 PM · swap

How do I swap positions of two objects smoothly?

I am making a game of finding the real bottle and in each turn the bottles swap positions in a triangular manner. They merge instead of swapping places and also the script is framerate based and I tried to do it with while loop and it swaps the positions in one frame.alt text

 using UnityEngine;
 using System.Collections;
 
 public class SwitchPlaces : MonoBehaviour 
 {
     public int speed=1;
     public GameObject[] bottles = new GameObject[6];
 
     // Use this for initialization
     void Start ()
     {
     
     }
     
     // Update is called once per frame
     void Update ()
     {
         Swap (bottles[0],bottles[1],bottles[2]);
     }
 
     void Swap(GameObject b1, GameObject b2, GameObject b3)
     {
         b1.transform.position = Vector3.MoveTowards(b1.transform.position
                                              ,b2.transform.position
                                              ,speed*Time.deltaTime);
 
         b2.transform.position = Vector3.MoveTowards(b2.transform.position
                                              ,b3.transform.position
                                              ,speed*Time.deltaTime);
 
         b3.transform.position = Vector3.MoveTowards(b3.transform.position
                                                     ,b1.transform.position
                                                     ,speed*Time.deltaTime);
     }
 }
screenshot (34).png (84.9 kB)
Comment
Add comment · Show 1
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 devilsid · Dec 26, 2014 at 07:51 AM 0
Share

You can use Vector3.Lerp ins$$anonymous$$d of Vector3.$$anonymous$$oveTowards

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Mehul-Rughani · Dec 26, 2014 at 08:20 AM

Hope You Are Looking For This....`

using UnityEngine; using System.Collections;

public class Testing : MonoBehaviour {

 public Transform test1;
 public Transform test2;
 Vector3 tempPos1;
 Vector3 tempPos2;
 // Use this for initialization
 void OnEnable () {
     tempPos1 = test1.position;
     tempPos2 = test2.position;
     StartCoroutine (Swap (10));
 }

 IEnumerator Swap(float time)
 {
     float i = 0;
     while (i<1) 
     {
         i+=Time.deltaTime/time;
         test1.position=Vector3.Lerp(test1.position,tempPos2,i);
         test2.position=Vector3.Lerp(test2.position,tempPos1,i);
         yield return 0;
     }
 }`
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 Kolibrizas · Sep 28, 2021 at 06:42 AM 0
Share

Should be

 test1.position=Vector3.Lerp(tempPos1,tempPos2,i);
 test2.position=Vector3.Lerp(tempPos2,tempPos1,i);

Otherwise, it is not a time based Lerp and it would visually finish up swapping pretty fast (let's say in half a second) meanwhile it would still wait 10 seconds or whatever time you set.

avatar image
0

Answer by Shinyclef · Dec 26, 2014 at 08:49 AM

Have a look at this line here:

 b1.transform.position = Vector3.MoveTowards(b1.transform.position
                                           ,b2.transform.position
                                           ,speed*Time.deltaTime);

Notices that b1 is moving towards b2. It is 'not' moving towards where b2 started out. This is why they are meeting in the middle. To prevent this, before you start moving the bottles, you should note their starting positions, and move towards that.

Here is a direct solution to your problem.

 using UnityEngine;
 using System.Collections;
 
 public class SwitchPlaces : MonoBehaviour
 {
     public int speed = 5;
     public GameObject[] bottles = new GameObject[6];
     public Vector3[] startingPositions = new Vector3[6];
 
     // Use this for initialization
     void Start()
     {
         SetStartingPositions();
     }
 
     void SetStartingPositions()
     {
         for (int i = 0; i < 6; i++)
         {
             startingPositions[i] = bottles[i].transform.position;
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         Swap(bottles[0], bottles[1], bottles[2]);
     }
 
     void Swap(GameObject b1, GameObject b2, GameObject b3)
     {
             b1.transform.position = Vector3.MoveTowards(b1.transform.position, startingPositions[1], speed * Time.deltaTime);
             b2.transform.position = Vector3.MoveTowards(b2.transform.position, startingPositions[2], speed * Time.deltaTime);
             b3.transform.position = Vector3.MoveTowards(b3.transform.position, startingPositions[0], speed * Time.deltaTime);
     }
 }

In Start, we set the starting positions. In Swap, we replace the current positions of the target bottles with their starting positions.

If you do it in a while loop, the whole loop will execute in a frame. Coroutines are perfect for when you want to split execution of that loop into multiple frames. They are also great because they don't get called every frames, only while they're still not finished.

This was kinda fun so I tool the liberty of extending it using coroutines. :) Below is my extended solution. Let me know if you have any questions :).

 using UnityEngine;
 using System.Collections;
 
 public class SwitchPlaces : MonoBehaviour
 {
     public int speed = 10;
     public int totalSwaps = 10;
     public GameObject[] bottles = new GameObject[6];
     
     private Vector3[] startingPositions = new Vector3[6];
     private int totalBottles;
 
     void Start()
     {
         totalBottles = bottles.Length;
         SetStartingPositions();
         StartCoroutine(RunSwaps());
     }
 
     void SetStartingPositions()
     {
         for (int i = 0; i < totalBottles; i++)
         {
             startingPositions[i] = bottles[i].transform.position;
         }
     }
 
     IEnumerator RunSwaps()
     {
         int swap = 1;
         int firstBottle = 0;
 
         while (swap <= totalSwaps)
         {
             yield return StartCoroutine(SwapPositions(firstBottle, (firstBottle + 1) % totalBottles, (firstBottle + 2) % totalBottles));
             swap++;
             firstBottle = (firstBottle + 1) % totalBottles;
             SetStartingPositions();
         }
     }
 
     IEnumerator SwapPositions(int a, int b, int c)
     {
         while (Vector3.Distance(bottles[a].transform.position, startingPositions[b]) > 0.01f ||
                Vector3.Distance(bottles[b].transform.position, startingPositions[c]) > 0.01f ||
                Vector3.Distance(bottles[c].transform.position, startingPositions[a]) > 0.01f)
         {
             bottles[a].transform.position = Vector3.MoveTowards(bottles[a].transform.position, startingPositions[b], speed * Time.deltaTime);
             bottles[b].transform.position = Vector3.MoveTowards(bottles[b].transform.position, startingPositions[c], speed * Time.deltaTime);
             bottles[c].transform.position = Vector3.MoveTowards(bottles[c].transform.position, startingPositions[a], speed * Time.deltaTime);
             yield return null;
         }
 
         bottles[a].transform.position = startingPositions[b];
         bottles[b].transform.position = startingPositions[c];
         bottles[c].transform.position = startingPositions[a];
     }
 }

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 marcozakaria · Sep 28, 2021 at 10:54 AM

you can use Dotween DoMove() functions

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Swapping Cameras Disables Keyboard Input 3 Answers

on touch swap texture, wait until sound played then swap texture back 1 Answer

Material Switching Upon Instantiation. over my head 0 Answers

Swapping one model for a prefab 2 Answers

SpriteRenderer animation generics? 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