Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Lerp Player position doesn't work

Discussion in 'Scripting' started by Apfelbox, Jul 15, 2014.

  1. Apfelbox

    Apfelbox

    Joined:
    Apr 28, 2014
    Posts:
    37
    Hey, Im currently working on a Game, where the player is controlled by swiping over the screen.
    The problem is that there is noc smooth lerping. I guess I'm using the command wrong?

    Here is my current code for translating the player:
    Code (csharp):
    1.  
    2. if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    3.        Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
    4.        fromPosition = player.transform.position;
    5.        toPosition = new Vector3(player.transform.position.x + Camera.main.ScreenToWorldPoint(touchDeltaPosition).x, transform.position.y + Camera.main.ScreenToWorldPoint(touchDeltaPosition).y, 0.0f);
    6.        player.transform.position = Vector3.Lerp(fromPosition, toPosition, speed);
    7.      }
    8.  
    Thats my previous code:
    Code (csharp):
    1.  
    2. if (Input.GetMouseButton (0)) {
    3.        Vector3 worldPos = new Vector3 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y, 0.0f);
    4.        player.transform.position = Vector3.Lerp (player.transform.position, worldPos, 0.05f);
    5.      }
    6.  
    With that code the player smoothly translates over the screen. The farther you touch the screen from the player the faster the player gets. That worked pretty well, but the problem with that solution was that the User constantly covers the player with his hand at points where you have to play slow and careful.

    short:
    I want to swipe in one direction and the player should smoothly translate in the direction swiped not affected by the position where I swipe.


    thanks in advance
    - apfelbox
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Lerp is useful if you know that you would like to go from A to B where they are both unchanged and only the interpolation factor changes. If you are using changing positions, you should use SmoothDamp:
    http://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

    Code (csharp):
    1. player.transform.position = Vector3.SmoothDamp (player.transform.position, toPosition, ref playerVelocity, smoothTime);
    playerVelocity and smoothTime need to be instance variables, such that the values are stored between frames. It is at least essential for palyerVelocity. You need to find a good value for smoothTime, you may try to use around 0.4 at first.

    Edit: Changed my code which accidentally used Lerp instead of SmoothDamp :oops:
     
    Last edited: Jul 15, 2014
  3. Apfelbox

    Apfelbox

    Joined:
    Apr 28, 2014
    Posts:
    37
    Thanks a lot!
    I'll try this immediatly :)
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I accidentally used Lerp instead of SmoothDamp in the posted example. I have already updated it.