Search Unity

Making interpolation check event-driven, instead of continuous

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, Jul 27, 2015.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    I'm trying to implement free flow combat (think arkham asylum), and one of the core building blocks of the system is adding movement to the attack- once I determine who the player is targeting, if that enemy is in range, I want to interpolate the player's transform from their current position to the enemy's position over a fixed duration, say 0.5s, then ensure that every attack starts with 0.5s of windup that will hide/dress up the movement.

    The most obvious way to do this seems to be something like
    Code (csharp):
    1.  
    2. void Update(){
    3. if (bInterpToEnemy){
    4. playerObject.position = Vector3.Lerp(start.position, end.position, ((Time.time - startTime)*speed)/length);
    5. }
    6. }
    7.  
    The problem is, you're only going to want to interpolate while attacking, which will be less than 20% of the game's total runtime, so 80% of the time I'll be making a useless check every frame - is there any way I can get this in a self-contained function, given the fact that location interpolation needs to be execute over multiple frames to look natural?
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    What you're looking for are called coroutines.
     
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    691
    Ooh that works perfectly, thank you! :)