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 Maicol-Face · Apr 30, 2015 at 07:53 AM · dragswap

Problem with drag an object and swap position with other

in a grid of cube i can select one of them and drag where i want. all the cube have a collider. when a cube collides with other, the other swap its position. if the swapping is static it's ok, but if i try to animate the movement something is wrong: if i move my finger very fast sometime the second cube lost the exactly position. i try with recast but in my case it doesn't works. i try to limited the velocity of the first cube but not works. i try to deactivate the collider while the second object swap its position but nothing. i think it's a problem of collider (it's like if i move the object very fast it collide with more than one object so it lost the right position) but i can't understand how i can solve it.

this is the code to drag the first object:

 if (dragging && touch.phase == TouchPhase.Moved)
              {
                  v3 = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
                  v3 = Camera.main.ScreenToWorldPoint(v3);    
  
                      toDrag.position = Vector3.Lerp (off,(v3+offset),1);
              }       

                  

this is the code to swap the position of second cube (the script is attached on the first cube):

  void OnTriggerEnter(Collider other)
      {
  
  // STATIC SWAP OK!
  
  //        if(other.GetComponent<Collider>().gameObject.name!="Cube")
  //        {
  //            PosTemp=other.transform.position;
  //            other.transform.position=Pos;
  //            Pos=PosTemp;
  //        }
  
  //ANIMATION BAD!
  
          if(other.GetComponent<Collider>().gameObject.name!="Cube")
          {
              if(beginning)
              {    
                  beginning=false;
                  PosTemp=other.transform.position;
                  PosFinal=Pos;
                  PosFinal.x = Mathf.Round (PosFinal.x);
                  PosFinal.y = Mathf.Round (PosFinal.y);
                  StartCoroutine(swapPosition(other.GetComponent<Collider>().gameObject,other.transform.position,PosFinal));
                 Pos=PosTemp; 
              }
          }
      }
      
      void OnTriggerStay(Collider other)
      {
  
      }
      void OnTriggerExit(Collider other)
      {
          beginning=true;
      }
      
      public IEnumerator swapPosition(GameObject obj, Vector3 posO, Vector3 posF)
      {
          time=0;
                 while (obj.transform.position!=posF)
                  {
                      //GetComponent<Collider>().enabled=false;
                      time += 5 * Time.deltaTime;
                      obj.transform.position = Vector3.Lerp(posO, posF, time);
                      yield return null;
                  }
          //collider.enabled=true;
      }
 

someone can help me please.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Maicol-Face · May 01, 2015 at 04:52 PM

ok i change completely strategy. now i put the game objects of the grid in a list and if i drag the first object in a different position i can take the second game object and swap with the first.

so the first time i used a sphereCollider but nothing. the second time the raycast but nothing. now i use a list but....NOTHING!!!! But finally i understand where is the problem: is the coroutine: if i move my finger very fast is like the coroutine doesn't finish and the second object take a different position.

 void Awake()
     {    
         GM=GameObject.Find ("GameManager");
         PuyoList = GM.GetComponent<GameManager>().GeneralList;
         Pos = new Vector3(Mathf.Round(gameObject.transform.position.x),Mathf.Round (gameObject.transform.position.y),0);
         first = Index(Pos);    
     } 
 
     void Start()
     {
         source=GetComponent<AudioSource>();
         newPos=Pos;
     }    
 void Update()
     {
         
         newPos = new Vector3(Mathf.Round(gameObject.transform.position.x),Mathf.Round (gameObject.transform.position.y),0);
         
         if(newPos != Pos)
         {
             source.Play();
             print("NewPos: " + newPos);
             print("Pos: " + Pos);
             
             second = Index(newPos);
             
             other=PuyoList[second];
             appo=PuyoList[first];
             
             StartCoroutine(swapPosition(other,newPos,Pos));
             
             PuyoList[first] = other;
             PuyoList[second] = appo;
 
             Pos=newPos;
             first = Index(Pos);
             
         }
     }    
     
 //LOOK HERE!!!
     public IEnumerator swapPosition(GameObject obj, Vector3 posO, Vector3 posF)
     {
         while (time < 1)
         {
             time +=  50*Time.deltaTime;
             obj.transform.position = Vector3.Lerp(posO, posF, time);
             yield return null;
         }
         
     }

if i put a high value of time (like 100) in the coroutine everything is ok, but if i put a low value (like 5) i have my problem.

same help about, please??

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 Maicol-Face · May 04, 2015 at 08:31 AM 0
Share

ok i solved my problem: in the Coroutine:

  public IEnumerator swapPosition(GameObject obj, Vector3 posO, Vector3 posF)
      {
          while (time < 1)
          {
              time +=  50*Time.deltaTime;
              obj.transform.position = Vector3.Lerp(posO, posF, time);
              yield return null;
          }
      }

each time i call the Coroutine the value of float "time" became 0, so my object can't reach the right position. but if i pass the float time like a parameter of the coroutine:

public IEnumerator swapPosition(GameObject obj, Vector3 posO, Vector3 posF, float time)

everything is ok cause i'm sure the value goes from 0 to 1. i hope to express myself correctly. Bye

avatar image
0

Answer by nbkhanhdhgd · Jul 04, 2019 at 09:18 AM

Hi @Maicol-Face

Thank you for your answer.

But can you give me full source code. Hope to receive the answer from you soon.

Thank you very much.

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

How to swap gameObject during EventSystem drag? 1 Answer

Drag and Swap two object using touch 0 Answers

Passing stuff from script to script (setting change-able waypoints) .... 1 Answer

Is there an Editor Window "IsSelected" equivalent for popup windows? 2 Answers

ball shooting passing through 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