- Home /
Continue object in the current direction of travel
Hello, this is all centered in 2D. So even know I am utilizing Vector3s, It's all Vector2 Space.
Currently, I have it on my mobile device where when I touch, a cube is lerped to the touch position. But then it stops. I'd like to have the cube, once within 1 meter (already setup) from the touch position to then continue, and not stop, on it's 2D course.
I've tried a lot of things, and a lot of questions, and a lot of answers. But none seem to satisfy a 2D scenario? Thanks in advanced.
Vector3 dir = touchPosition - transform.position;
rigidbody2D.AddForce(dir * 5);
This just makes the cube 100% erratic, going in random directions. :l
Answer by DanSuperGP · Jan 28, 2015 at 12:29 AM
Use physics...
Add a rigidbody to the cube
Rather than moving the transform with a lerp, add force in the direction you want it to travel.
Here, you're going to simple. I've been working with Unity for years I would have tried that first.
The problem is, I can't think of a calculation that could provide me with the 2D current direction of travel in a local sense. I can't launch it forward without knowing the direction to launch it in? How can I find the current direction of travel, to then use it how you said.
uh... you already calculated the vector in your code...
touchposition - transform.position...
just normalize it... multiply it by the force you want to add...
That's what I thought but it just glides in a different position relative to 3D. I don't understand it :l It's been confusing me for so long now. All the logic is correct, but it won't travel.
This just makes it erratic actually. It goes in different directions depending on where the touch is.
Vector3 dir = touchPosition - transform.position;
rigidbody.AddForce(dir * 5);
Edit : on further notice, the cube perfectly orbits the touch position :l
the vector doesn't change per frame. Nor, well, ever. The code has changed since you last saw
Regardless, any other reason this would be happening?
$$anonymous$$y bad, my IDE didn't update the code :) Looks like changing the vector from an update position did the trick, interesting how I missed that. Thanks though! All fixed!
Answer by Andres-Fernandez · Jan 27, 2015 at 07:30 AM
Just start a coroutine that moves the cube for the remainder of the distance (the same lerp, but inside a coroutine). Manual reference, API reference.
That's not what I was asking. I need to know how to get it to move in its current path once its PASSED the touch :)