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);
}
}
}