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

[SOLVED] Scripting a Dodge Skill - Something like Summoner's Spell Flash, from League of Legends

Discussion in 'Scripting' started by frozendog_bren, Mar 2, 2016.

  1. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    Whenever the player presses a button, the character should dodge towards the mouse (like rolling), but if the mouse is too distant from the player, exceeding the skill range, he should go just for the max range. I managed to create it using Vector3.Lerp, but yet i don't know how to limit it and, for now, the player can travel through the whole area if the mouse cursor reaches.

    What i need is something very similar to LoL's Flash. When the mouse cursor is to far way, the player still can use the skill, but he dodges to its max range.

    Code (CSharp):
    1.  
    2.  
    3. Vector3 dashTarget;
    4.  
    5. void Skill_DashUpdate() //called every frame
    6.     {
    7.         if(Input.GetKeyDown(KeyCode.D))
    8.         {
    9.             Plane plane = new Plane (Vector3.up, transform.position);
    10.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    11.             float point = 0f;
    12.      
    13.             if(plane.Raycast(ray, out point))
    14.                 dashTarget = ray.GetPoint(point);
    15.      
    16.             Invoke("Skill_Dash", 0.05f);
    17.         }
    18.     }
    19.  
    20.     void Skill_Dash()
    21.     {
    22.         if(pbtScript.staminaPoints >= dashStaminaCost)
    23.         {
    24.             transform.position = Vector3.Lerp(transform.position, dashTarget, 1f);
    25.             transform.LookAt(dashTarget);
    26.         }
    27.     }
    -x-

    Solution Script
    Special thanks to mathiasj!
    Code (CSharp):
    1.  
    2.  
    3. Vector3 dashTarget;
    4. float dashRadius = 3f
    5.  
    6. void Skill_DashUpdate()
    7.     {
    8.         if(Input.GetKeyDown(KeyCode.D))
    9.         {
    10.             Plane plane = new Plane (Vector3.up, transform.position);
    11.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    12.             float point = 0f;
    13.          
    14.             if(plane.Raycast(ray, out point))
    15.                 dashTarget = ray.GetPoint(point);
    16.          
    17.             Invoke("Skill_Dash", 0.05f);
    18.         }
    19.  
    20.     }
    21.  
    22.     void Skill_Dash()
    23.     {
    24.         if(pbtScript.staminaPoints >= dashStaminaCost)
    25.         {
    26.             if(Vector3.Distance(transform.position, dashTarget) <= dashRadius)
    27.             {
    28.                 transform.position = Vector3.Lerp(transform.position, dashTarget, 1f);
    29.             }
    30.  
    31.             else if(Vector3.Distance(transform.position, dashTarget) > dashRadius)
    32.             {
    33.                 Vector3 dir = dashTarget - transform.position;
    34.                 dir.Normalize();
    35.                 dashTarget = transform.position + dashRadius * dir;
    36.          
    37.                 transform.position = Vector3.Lerp(transform.position, dashTarget, 1f);
    38.             }
    39.         }
    40.     }
     
    Last edited: Mar 2, 2016
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,141
    frozendog_bren likes this.
  3. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    Sure, but then, how to do a "don't go that far" and insert the player on the edge of the max range? At the moment it's easy to use Distance to say "it's too far" and block him from using the dodge. If the distance from player to target is bigger than the skill range, don't use dash, for example. Kinda bad...
     
  4. Craze74

    Craze74

    Joined:
    Nov 19, 2012
    Posts:
    83
    You need to check the length of your vector between the player and your mouse position, if the vector > maxLength then you tell the player the distance is too big
     
  5. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    Ok, but i need to put the player on the dodge's max range when the mouse cursor is too far. This i don't know how to do :(
     
  6. mathiasj

    mathiasj

    Joined:
    Nov 3, 2015
    Posts:
    64
    You get the direction vector from the mouse to the player (mouseVec - playerVec), normalize that and multiply it by the desired radius. The position of the player + the resulting Vector is the position after the dodge.

    So something along the lines of this:
    Code (CSharp):
    1.  
    2. //if radius too big
    3. Vector3 dir = ray.GetPoint(point) - player.transform.position;
    4. dir.Normalize();
    5. dashTarget = player.transform.position + radius * dir;
     
    frozendog_bren likes this.
  7. Craze74

    Craze74

    Joined:
    Nov 19, 2012
    Posts:
    83
    Take your player transform, take your mouse position on the plane you are using, cast a vector between the two, check the length, if the length is too big, don't allow the player to perform dash action

    Do we absolutely need to normalize the vector ?
     
  8. mathiasj

    mathiasj

    Joined:
    Nov 3, 2015
    Posts:
    64
    Yes, because he wants to move the player towards the mouse cursor with a limited radius. If the cursor is outside of the range, it should still move the player to the edge of that radius as far as I understood it.
     
    frozendog_bren likes this.
  9. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    EXACTLY!
     
  10. Craze74

    Craze74

    Joined:
    Nov 19, 2012
    Posts:
    83
    Alright didn't understood that part sorry ^^
     
  11. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    Np. Thanks for your time!
     
  12. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19

    Works perfectly. Thank you very much.