Search Unity

How to move from one point to another when I press a button in x-axis in 2d

Discussion in 'Scripting' started by blackhawk9, Feb 8, 2016.

  1. blackhawk9

    blackhawk9

    Joined:
    Feb 8, 2016
    Posts:
    2
    I am trying to translate the position of an object from one point to second in x-axis in 2d while it is moving in y direction continuously but after moving only one time it is getting struck plsss help me
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Get the current position of the transform and then just modify the X position using Lerp if you want a smooth transition and then just set the transform again.

    Code (CSharp):
    1.         Vector3 position = this.transform.position;
    2.         position.x = Mathf.Lerp(position.x, target.x, speed * Time.deltaTime);
    3.         this.transform.position = position;
     
  3. blackhawk9

    blackhawk9

    Joined:
    Feb 8, 2016
    Posts:
    2
    OK I will try this thanks for the reply