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

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.

Vector3 dashTarget;

void Skill_DashUpdate() //called every frame
    {
        if(Input.GetKeyDown(KeyCode.D))
        {
            Plane plane = new Plane (Vector3.up, transform.position);
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            float point = 0f;
     
            if(plane.Raycast(ray, out point))
                dashTarget = ray.GetPoint(point);
     
            Invoke("Skill_Dash", 0.05f);
        }
    }

    void Skill_Dash()
    {
        if(pbtScript.staminaPoints >= dashStaminaCost)
        {
            transform.position = Vector3.Lerp(transform.position, dashTarget, 1f);
            transform.LookAt(dashTarget);
        }
    }

-x-

Solution Script
Special thanks to mathiasj!

Vector3 dashTarget;
float dashRadius = 3f

void Skill_DashUpdate()
    {
        if(Input.GetKeyDown(KeyCode.D))
        {
            Plane plane = new Plane (Vector3.up, transform.position);
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            float point = 0f;
          
            if(plane.Raycast(ray, out point))
                dashTarget = ray.GetPoint(point);
          
            Invoke("Skill_Dash", 0.05f);
        }

    }

    void Skill_Dash()
    {
        if(pbtScript.staminaPoints >= dashStaminaCost)
        {
            if(Vector3.Distance(transform.position, dashTarget) <= dashRadius)
            {
                transform.position = Vector3.Lerp(transform.position, dashTarget, 1f);
            }

            else if(Vector3.Distance(transform.position, dashTarget) > dashRadius)
            {
                Vector3 dir = dashTarget - transform.position;
                dir.Normalize();
                dashTarget = transform.position + dashRadius * dir;
          
                transform.position = Vector3.Lerp(transform.position, dashTarget, 1f);
            }
        }
    }

Check the distance between the player’s position and the target?

http://docs.unity3d.com/ScriptReference/Vector3.Distance.html

1 Like

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…

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

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 :frowning:

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:

//if radius too big
Vector3 dir = ray.GetPoint(point) - player.transform.position;
dir.Normalize();
dashTarget = player.transform.position + radius * dir;
1 Like

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 ?

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.

1 Like

EXACTLY!

Alright didn’t understood that part sorry ^^

Np. Thanks for your time!

Works perfectly. Thank you very much.