Search Unity

Moving object towards a position instead of jumping to position

Discussion in 'Physics' started by Crillst, Feb 13, 2015.

  1. Crillst

    Crillst

    Joined:
    Dec 29, 2013
    Posts:
    69
    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:

    Code (CSharp):
    1.     private void HandleSteering()
    2.     {
    3.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    4.             Vector3 rawPosition = gameCamera.ScreenToWorldPoint(Input.mousePosition);
    5.             Vector3 targetPosition = new Vector3(rawPosition.x, -2.128f, 0.0f);
    6.             float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
    7.             targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);      
    8.             rigidbody2D.MovePosition(targetPosition);
    9.         }
    10.     }
    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.
     
  2. Crillst

    Crillst

    Joined:
    Dec 29, 2013
    Posts:
    69
    Solved the jump in the movement.
    The whole time i was using the touch input position and moved my gameobject to that position. therefor long movements seemed like a jump.

    The correct way was to use the deltaposition and append that to my gameobjects position instead. Just as i said that i tried in the original post, but this time i multiplied the deltaposition by 0.04f so that the gameobject didn't move to fast from side to side. The code looks like this if anyone else gets stuck:

    Code (CSharp):
    1.     private void HandleSteering()
    2.     {
    3.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    4.             Vector3 movement = Input.GetTouch(0).deltaPosition;
    5.             Vector3 targetPosition = new Vector3(rigidbody2D.transform.position.x + (movement.x * this.moveSpeed), -2.128f, 0.0f);
    6.             float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
    7.             targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
    8.             rigidbody2D.MovePosition(targetPosition);
    9.         }
    10.     }