in Education by
I have an object that I want to move by swipe, for example when the swipe is up the object should move forward smoothly from point A to point B, swipe right the object move smoothly to the right etc... To do that, I've tried Lerp, MoveTowards and SmoothDamp but every time the object just disappear from point A and appear on point B instantly. So I used coroutine to give a time to the movement, and as you can see in the code bellow, there's 4 coroutine methods, each one is for a direction. the problem I have is that when playing, the first movement work properly, but in the second swipe the object didn't reach the destination point, and the third one also and the object have some weird movements. Can you tell me what's wrong in my code? Here's the Coroutine methods for movements: public IEnumerator MoveForward() { Vector3 DestinationF = new Vector3(transform.position.x, transform.position.y, transform.position.z + DistanceF); while (Vector3.Distance(transform.localPosition, DestinationF) > 0) { float totalMovementTimeF = 0.3f; float currentMovementTimeF = 0f; currentMovementTimeF += Time.deltaTime; transform.localPosition = Vector3.Lerp(transform.position, DestinationF, currentMovementTimeF / totalMovementTimeF); yield return null; } } public IEnumerator MoveBackward() { Vector3 DestinationB = new Vector3(transform.position.x, transform.position.y, transform.position.z - DistanceB); while (Vector3.Distance(transform.localPosition, DestinationB) > 0) { float totalMovementTimeB = 0.3f; float currentMovementTimeB = 0f; currentMovementTimeB += Time.deltaTime; transform.localPosition = Vector3.Lerp(transform.position, DestinationB, currentMovementTimeB / totalMovementTimeB); yield return null; } } and there is still 2 coroutine methods MoveRight() and MoveLeft(). And here's the code for the swipe directions: if (Input.GetMouseButtonDown(0)) { //save began touch 2d point firstPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y); } if (Input.GetMouseButtonUp(0)) { //save ended touch 2d point secondPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y); //create vector from the two points currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y); //normalize the 2d vector currentSwipe.Normalize(); // swipe up if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) { StartCoroutine(MoveForward()); } // swipe down if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) { StartCoroutine(MoveBackward()); } //swipe left if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) { StartCoroutine(MoveLeft()); } //swipe right if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) { StartCoroutine(MoveRight()); } } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Your first Coroutine works because: Vector3 DestinationF = new Vector3(transform.position.x, transform.position.y, transform.position.z + DistanceF); will result in a positive position, so, the distance will be greater than 0: while (Vector3.Distance(transform.localPosition, DestinationF) > 0) On the other hand, while subtracting the distanceB from the z value: Vector3 DestinationB = new Vector3(transform.position.x, transform.position.y, transform.position.z - DistanceB); may result in a negative value, therefore: while (Vector3.Distance(transform.localPosition, DestinationB) > 0) will start as < 0, so the condition is never met. Check your condition. Do you want absolute values, or not equal to 0?

Related questions

0 votes
    My good ladies and gentlemen, I recently had my first go at Worker services in .Net Core 3.1, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I am investigating the design of a work queue processor where the QueueProcessor retrieves a Command Pattern object ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    We have some .NET code that does a call out to a COM component (dnsapi.dll) to query whether a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Howdy, I have a DataRow pulled out of a DataTable from a DataSet. I am accessing a column that is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    Greetings all, I'm trying to localize a .NET/C# project. I'm using string resource files and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    We ran into a magic decimal number that broke our hashtable. I boiled it down to the following minimal ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    We ran into a magic decimal number that broke our hashtable. I boiled it down to the following minimal ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I want create expression with reflection in c#. I am target script is: using (var service = new ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I want to make a method that will populate the lst_List list with rows from various tables and fields. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I want to make a method that will populate the lst_List list with rows from various tables and fields. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I am trying to call a method from the script Dice and after that method is executed the value of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    C#6 Update In C#6 ?. is now a language feature: // C#1-5 propertyValue1 = myObject != null ? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
...