Search Unity

Vector3.Lerp not smoothly working, making character appear interpolating everypoint

Discussion in 'Scripting' started by UnlimitedEdition, Nov 29, 2015.

  1. UnlimitedEdition

    UnlimitedEdition

    Joined:
    Feb 27, 2012
    Posts:
    287
    I'm trying to make a character smoothly move from its own position to a point I click at. The code is seemingly working in the console but for whatever reason the character only moves when the entire loop in the "smoothMove" function has terminated and just appears at the destination instead of smoothly translating to the intended destination. It's confusing that I can see the character position changing when I print it in the console but the character only moves when the loop terminates.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.     public GameObject character;
    6.     public float speed = 1.0f;
    7.  
    8.     private RaycastHit hit;
    9.     private Vector3 travelPoint;
    10.     private Vector3 startPoint;
    11.     private float travelDistance;
    12.     private float startTime;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update () {
    21.         if (Input.GetButtonDown("Fire2"))
    22.         {
    23.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
    24.             {
    25.                 Debug.Log(hit.point);
    26.                 travelPoint = new Vector3(hit.point.x, 1.5f, hit.point.z);
    27.                 startPoint = character.transform.position;
    28.                 travelDistance = Vector3.Distance(startPoint, travelPoint);
    29.  
    30.                 smoothMove(character.transform, startPoint, travelPoint, Time.time);
    31.  
    32.                 //character.transform.position = new Vector3(hit.point.x, 1.5f, hit.point.z);
    33.             }
    34.         }
    35.     }
    36.  
    37.     void smoothMove(Transform chr, Vector3 startPos, Vector3 endPos, float time)
    38.     {
    39.         float i = 0.0f;
    40.         float rate = 1.0f / time;
    41.         while(i < 1.0)
    42.         {
    43.             i += 0.005f;
    44.             chr.position = Vector3.Lerp(startPos, endPos, i);
    45.             print(character.transform.position);
    46.         }
    47.     }
    48. }
     
    Last edited: Nov 29, 2015
  2. Manawydan

    Manawydan

    Joined:
    Nov 25, 2015
    Posts:
    30
    try this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Movement : MonoBehaviour {
    4.     public GameObject character;
    5.     public float speed = 1.0f;
    6.     private RaycastHit hit;
    7.     private Vector3 travelPoint;
    8.     private Vector3 startPoint;
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if (Input.GetButtonDown("Fire2"))
    17.         {
    18.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
    19.             {
    20.                 Debug.Log(hit.point);
    21.                 travelPoint = new Vector3(hit.point.x, 1.5f, hit.point.z);
    22.                 startPoint = character.transform.position;
    23.                 character.transform.position = Vector3.Lerp(startPoint,travelPoint,speed*Time.deltatime);
    24.             }
    25.         }
    26.     }
    27. }
     
  3. UnlimitedEdition

    UnlimitedEdition

    Joined:
    Feb 27, 2012
    Posts:
    287
    This doesn't work because then the character travels to the first interpolated vector but doesn't continue to travel to more interpolated vectors.
     
  4. Lentaq

    Lentaq

    Joined:
    Apr 8, 2015
    Posts:
    57
    Several things of note here:

    1.) You are using a while loop in your SmoothMove method. You shouldn't use while loops outside of co-routines, really. This is why you are seeing that jump from start to finish when the loop terminates. The calculations in your while loop are being performed, but the "while" is freezing everything else up, so that nothing happens until the loop is finished.

    2.) If you are wanting to be able to hold the button down and be able to drag it around to move, use Input.GetButton instead of GetButtonDown.(I don't know if you are trying to do that, but just thought I'd mention it). Lerp wouldn't be the right way to go about that though.

    3.)
    a.) Generally, the way Lerp is used wrong. The way Manawydan posted should use Vector3.MoveTowards instead of Lerp.

    b.) The way you are trying to do the movement, I think Lerp is the right command, but your implementation is a little off and I recommend checking out: How to Lerp Correctly

    It looks like you had the right idea with having your startTime and rate variables and such, but you didn't follow through with it and kind of abandoned those.
     
    Manawydan likes this.