- Home /
Getting a position behind a target object (targeted attack follow through)
I am trying to code a dash attack, where the player dashes through the target enemy.
I've attached a quick diagram showing the part im stuck on.
Basically I have the player and enemy positions, and im trying to find a Target Position behind the enemy based on an adjustable follow through distance.
Any help/suggestions are greatly appreciated <3
"Direction" is the key here, get the direction! @A$$anonymous$$eKitty (Direction = Destination - Origin).
"target position" is equal to "enemy position" plus "your direction towards enemy times some numbers"
var direction = enemyPosition - myPosition;
var targetPosition = enemyPosition + (direction.normalized * followThroughDistance);
Debug.Log("Target position is :" + targetPosition);
Thanks, will try implement this tonight <3
Answer by PhyrePhrost · Jun 21, 2021 at 07:56 AM
you can use the condition of physics.raycast (DOC: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html), simply you draw a ray to the dash direction and if the player hit the enemy, he was attacked
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, dashDistance)
{
//you can use a custom tag to check if the hit was an enemy
if(hit.gameobject.tag == "Enemy")
Debug.Log("The enemy was attacked");
}
//here you will move the player to the dash point
I hope this can help your question :)
This finds the point of collision forward of the game object that calls it.
It doesn't find the position opposit the enemy from the player at a given distance.
You need some basic vector math here, which I will note in an answer below but in short the steps would be.
Find the heading to the enemy from the player
using the heading project a point along that direction past the enemy by the follow thru distance e.g. enemy.pos + (heading.Normalized * fopllowThru);
feed that target vector into your dash attack
Answer by lodendsg · Jun 21, 2021 at 09:00 AM
To find the heading to any vector from another its simply
heading = subject - self;
So the heading vector (a vector which is in the direction of the subject from your self and has a length equal to the distance between you and the subject ... would be
var heading = enemy.position - player.position;
You can now set your target position according to that direction e.g.
var targetPos = enemy.position + (heading.Normalized * followThruDiistance);
Your answer
Follow this Question
Related Questions
Getting the distance in a known direction 2 Answers
Why is the method being called every frame even though it is InvokeRepeating? 1 Answer
Multiple Cars not working 1 Answer
Weird angle returned 1 Answer
Distribute terrain in zones 3 Answers