Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity Vector2.Lerp

Discussion in 'Scripting' started by 01zabil01, Oct 8, 2015.

  1. 01zabil01

    01zabil01

    Joined:
    Oct 8, 2015
    Posts:
    5
    Hello, im pretty new to unity and for beggining im trying to make 3-road runner like subway surfers but not with such good graphic. I want player to move smoothly and i've written something like this:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class movement : MonoBehaviour {
    6.  
    7.    
    8.    
    9.  
    10.     private Vector2 positionC;
    11.     private Vector2 positionB;
    12.     private Vector2 positionA;
    13.  
    14.  
    15.  
    16.  
    17.  
    18.  
    19.     void Start () {
    20.         speed = 100f
    21.  
    22.         //positionA is left road, positionB is middle road and positionC is right road
    23.  
    24.         Vector2 positionA = new Vector2(0.86f,-1.863989f);
    25.         Vector2 positionB = new Vector2(2.59f,-1.863989f);
    26.         Vector2 positionC = new Vector2(4.37f,-1.863989f);
    27.  
    28.     }
    29.    
    30.    
    31.     void Update () {
    32.         if(Input.GetKeyDown(KeyCode.D)) {
    33.         transform.position = Vector2.Lerp(positionB, positionC, 10);
    34.         }
    35.  
    36.    
    37.            
    38.    
    39.     }
    40. }
    41.  
    I know it doesn't work how it should but it is just a test for using lerp. When i press D, object coordinates set to 0,0,0. When i press D, player should change position from middle-road to right-road. Where have i made mistake?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Two issues here:

    A) The third parameter of a Lerp function is from 0.0 to 1.0. At 0.0 (or less) it will return the left input, and at 1.0 (or more) it will return the right input. Not sure what your intentions are in sending it 10, but only 0 through 1 matter.

    B) in Start(), you aren't modifying your three position member variables. You're creating three new, local variables and setting them; the member variables still have their default value of (0,0,0). Remove the first "Vector2" from those lines.

    And a suggestion: Instead of hard-coding position values, make those public variables. You'll be able to edit their values in the inspector. Better yet, use public references to Transforms; you can create "leftRoad" and "rightRoad" GameObjects, set their positions graphically in the editor, and use leftRoad.position and rightRoad.position as your Lerp inputs.

    Code (csharp):
    1.  
    2. public Transform leftRoad;
    3. public Transform rightRoad;
    4. void Update() {
    5. if (Input.GetKeyDown(KeyCode.D) ) {
    6. transform.position = Vector3.Lerp(leftRoad, rightRoad, 1f);
    7. }
    8. }
     
    Kiwasi likes this.
  3. 01zabil01

    01zabil01

    Joined:
    Oct 8, 2015
    Posts:
    5
    Now it says it cannot be converted from UnityEngine.Trasform to UnityEngine.Vector.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Oh, sorry, my bad. leftRoad.position, rightRoad.position.