Search Unity

Lerp Moving Instantly

Discussion in '2D' started by heytheremogwai, Oct 1, 2019.

  1. heytheremogwai

    heytheremogwai

    Joined:
    Mar 22, 2018
    Posts:
    36
    I'm going to refactor all of this code...but for now, I'm trying to figure out why my creature is lerping to the destination immediately (no matter what I set the default_speed to).

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class creature : MonoBehaviour
    7. {
    8.     public float default_speed = 0.01f;
    9.     private Rigidbody rb;
    10.     private bool moved = false;
    11.     private Vector3 destination;
    12.     private int x, y;
    13.     public Vector3 current_pos;
    14.     float speed;
    15.  
    16.     void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.         current_pos = rb.transform.position;
    20.         x = Random.Range(-15, 16) * 10;
    21.         y = Random.Range(-15, 16) * 10;
    22.         destination.x = x;
    23.         destination.y = y;
    24.         destination.z = 0;
    25.     }  
    26.  
    27.     void Update()
    28.     {
    29.         patrol(); //non-aggro movement      
    30.     }
    31.  
    32.     void patrol()
    33.     {
    34.         speed = Time.deltaTime * default_speed;
    35.         Debug.Log(speed);
    36.         transform.position = Vector3.Lerp(transform.position, destination, speed);
    37.     }
    38. }
    39.  
    40.  
    speed is logging at around 0.03 in this instance....
     
    Last edited: Oct 2, 2019
  2. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
    Comment out:
    Code (CSharp):
    1. moved = true;
    in if (moved == false){...}
    If object moves as needed set moved=true somewhere else.