- Home /
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.
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??
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
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.
Your answer
Follow this Question
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