Hi there!
Im starting to learn some physics in Unity, and im starting with a 2D Game.
I’ve created a rocket that is supposed to be controlled by touch input. So far i’ve got this code:
private void HandleSteering()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector3 rawPosition = gameCamera.ScreenToWorldPoint(Input.mousePosition);
Vector3 targetPosition = new Vector3(rawPosition.x, -2.128f, 0.0f);
float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
rigidbody2D.MovePosition(targetPosition);
}
}
What this does is checking the input of the mouseposition(this also works with touch input). it creates a targetposition. After that i clamps the targetwidth so the rocket wont exit the screen. And this works almost as i want it to.
However, if the rocket is to the left of the screen, and i touch and move with my finger on the right side of the screen, my object will jump to this position, instead of moving to the place which the input was dragged.
What i’m looking for is if i put a finger on the right side of the screen, i want to move my object as much as the input was moved, and not making it jump to that position.
I’ve tried the Vector3.MoveTowards and this works, but the movement is very sluggish.
I’ve also tried using Input.GetTouch(0).deltaPosition, but i couldn’t figure out how to use it because as fast as i touched the screen to steer the rocket would get positioned to the side of the screen.