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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to interupt a Lerp movement from A to B,for attack and then resume the lerp mvement

Discussion in 'Scripting' started by Like-A-Sir, Dec 22, 2015.

  1. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Good day!

    So I have a flying enemy which moves from A to B using Lerp(Eased Movement).The idea is that the enemy has a child which represents just a collider(I think this are Compound Colliders).Anyway , when the player is triggered by the collider I want my flying enemy to attack in the position where the player was located during the last trigger.

    The problem is that I don't know how to stop the Lerp process and start another Lerp to the player(the attack).And if the player doesn't kill the enemy I want it to resume the horizontal Lerp from the begining.(During the attack only the Y coordonate suffers modifications).

    Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WaypointSystem : MonoBehaviour {
    5.  
    6.     public Vector3[] localWaypoints;
    7.     Vector3[] globalWaypoints;
    8.  
    9.     public float speed;
    10.     [Range(0,2)]
    11.     public float easeAmount;
    12.  
    13.     int fromWaypointIndex;
    14.     float percentBetweenWaypoints;
    15.  
    16.     void Start(){
    17.         globalWaypoints = new Vector3[localWaypoints.Length];
    18.         for (int i=0; i < localWaypoints.Length; i++) {
    19.             globalWaypoints[i] = localWaypoints[i] + transform.position;
    20.         }
    21.     }
    22.  
    23.  
    24.     void Update(){
    25.  
    26.         Vector3 velocity = CalculatePlatformMovement ();
    27.  
    28.         transform.Translate (velocity);
    29.  
    30.     }
    31.  
    32.     float Ease(float x){
    33.         float a = easeAmount + 1;
    34.         return Mathf.Pow (x,a) / (Mathf.Pow(x,a) + Mathf.Pow(1-x,a));
    35.     }
    36.  
    37.     Vector3 CalculatePlatformMovement(){
    38.         int toWaypointIndex = fromWaypointIndex + 1;
    39.         float distanceBetweenWaypoints = Vector3.Distance (globalWaypoints[fromWaypointIndex],globalWaypoints[toWaypointIndex]);
    40.         percentBetweenWaypoints += Time.deltaTime * speed / distanceBetweenWaypoints;
    41.         percentBetweenWaypoints = Mathf.Clamp01 (percentBetweenWaypoints);
    42.         float easedPercentBetweenWaypoints = Ease (percentBetweenWaypoints);
    43.  
    44.         Vector3 newPos = Vector3.Lerp (globalWaypoints [fromWaypointIndex], globalWaypoints [toWaypointIndex], easedPercentBetweenWaypoints);
    45.  
    46.         if (percentBetweenWaypoints >= 1) {
    47.             percentBetweenWaypoints = 0;
    48.             fromWaypointIndex++;
    49.             if(fromWaypointIndex >= globalWaypoints.Length - 1){
    50.                 fromWaypointIndex = 0;
    51.                 System.Array.Reverse(globalWaypoints);
    52.             }
    53.         }
    54.  
    55.  
    56.         return newPos - transform.position;
    57.     }
    58.  
    59.     void OnDrawGizmos(){
    60.         if (localWaypoints != null) {
    61.             Gizmos.color = Color.white;
    62.             float size = .3f;
    63.  
    64.             for(int i=0; i < localWaypoints.Length; i++){
    65.                 Vector3 globalWaypointPos = (Application.isPlaying)?globalWaypoints[i] : localWaypoints[i] + transform.position;
    66.                 Gizmos.DrawLine(globalWaypointPos - Vector3.up * size, globalWaypointPos + Vector3.up * size);
    67.                 Gizmos.DrawLine(globalWaypointPos - Vector3.left * size, globalWaypointPos + Vector3.left * size);
    68.             }
    69.         }
    70.     }
    71.  
    72.     void OnTriggerEnter2D(Collider2D other){
    73.         if (other.tag == "Player") {
    74.             Debug.Log("The player is under the enemy!");
    75.         }
    76.     }
    77. }
    78.  
    Thank you!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    In your CalculatePlatformMovement() function you are using Time.deltaTime to "progress" through the percentBetweenWaypoints variable, from 0 to 1.

    When you want the Lerp to stop, simply freeze that value. You could make a bool flag that indicates when the enemy is supposed to freeze, and during that time, do not advance the percentBetweenWaypoints value, and the enemy should freeze in place.
     
    Like-A-Sir likes this.
  3. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Nice! Thank you! I will try this.