2

I have a script that currently swaps the position of 2 objects. The objects are the same size and shape so that when they swap positions, it's instant and you don't see them move toward the position. How can I make it so there is a movement speed when the two objects swap places, so that you can see them actually moving positions. I also need to figure out how to add a third object so that they all switch positions randomly. Something like this is my final goal: http://i249.photobucket.com/albums/gg240/OldNewby4507/shell-game-animated.gif

 using UnityEngine;
 using System.Collections;

 public class NewBehaviourScript : MonoBehaviour
 {

     public float movementSpeed = 10;
     public GameObject g1;
     public GameObject g2;
     public Vector3 vec1;
     public Vector3 vec2 = new Vector3(2F, 2F, 2F);
     public bool swapBack = false;

     void Start()
     {
         g1 = GameObject.Find("object1");
         g2 = GameObject.Find("object2");
         vec1 = new Vector3(g1.gameObject.transform.position.x, g1.gameObject.transform.position.y, g1.gameObject.transform.position.z);
         vec2 = new Vector3(g2.gameObject.transform.position.x, g2.gameObject.transform.position.y, g2.gameObject.transform.position.z);

     }

     void Update()
     {
         if (Input.GetMouseButtonDown(0))

         {
             vec1 = g1.gameObject.transform.position;
             vec2 = g2.gameObject.transform.position;
             g1.gameObject.transform.position = vec2;
             g2.gameObject.transform.position = vec1;
         }
     }
 }

right now my movementSpeed variable is unused.

Nicole Pinto
  • 364
  • 1
  • 22
  • 2
    Note that if you settled on one of the answers to your [previous question](http://stackoverflow.com/questions/41001473/how-do-i-get-an-object-to-move-and-swap-places-with-another-object-on-a-mouse-c), you should consider accepting one of them. Alternatively, supply your own answer to the question. – Serlite Dec 06 '16 at 20:13

3 Answers3

2

The way i would tackle this is the following:

You need to create a function which is called, for example, MovePath. MovePath will be governed by a formula that determines how the object moves from point A to point B (this can be linearly, exp, log, etc). MovePath takes three parameters, vector Start, vector End and int or double MoveSpeed. To get from point A to point B will take steps equal to MoveSpeed. So you are calculating positions at each step of the way.

Update will need to be modified so that it takes int MoveSpeed as a parameter. Update will keep updating by using the movepath method until vector start becomes vector end. You will be calling MovePath twice (one for each object thats moving) until for g1's position becomes g2's and g2's position becomes g1's. Update will track when G1 == G2 and G2 == G1 with respect to the positions and stop updating once that is complete.

You will need to implement async and await so that the UI can update. There might be a way to do this synchronously but i think going the async and await path will be much cleaner in the long run.

Please let me know if you have questions when you try to implement this. Once you try to implement this edit your question and I think people (and myself) will be able to chime in with issues you are running into when implementing this.

Edit: there are a lot of ways to implement this. An other example is you can change the MovePath so that it calls itself recursively until the end condition is met. I just wanted to provide you with an idea on how to tackle it.

peterpep
  • 314
  • 3
  • 11
1

You can Lerp the transition at difference rates, which is quite linear, if you would like you can also use Slerp or any of the other Vector3 methods.

You can also use libraries that offer Tweening operations such as iTween amongst a bunch of other ones on the asset store which will take care of the transition for you.

Landern
  • 374
  • 2
  • 5
1

I would use Lerp, or Slerp. These are interpolation methods using math, and are quite simple and seamless. Your code would look something like this:

 void Update() {
     if (Input.GetMouseButtonDown(0)) {
         vec1 = g1.gameObject.transform.position;
         vec2 = g2.gameObject.transform.position;
         g1.gameObject.transform.position = Vector3.Lerp(vec1, vec2, 0.5f); 
         g2.gameObject.transform.position = Vector3.Lerp(vec2, vec1, 0.5f);
     }
 }

Here is an excellent explanation of Lerp, it may help you in this case!

KenSchnetz
  • 206
  • 2
  • 12
  • 1
    I tried this code, they are still swapping "instantly", with no movement shown, is there a way to make the speed so that you can physically see the objects translating? – Nicole Pinto Dec 06 '16 at 23:50
  • I made a mistake, 10.0f should be a numer between 0 and 1, try 0.5f. – KenSchnetz Dec 07 '16 at 00:00
  • 1
    Alright, this works to "see the translation movement", but now instead of swapping places, the two items (let's call them cubes), meet up at the point exactly in middle of their starting positions until they are overlapping. They should be exchanging places completely and I'm not sure how to go about fixing that. – Nicole Pinto Dec 07 '16 at 00:22
  • Ok, change the 0.5f to 1.0f, that should do it... sorry Lerp is confusing sometimes! – KenSchnetz Dec 07 '16 at 00:23