Get position near gameobject


I am trying to come up with a script that gets the position of a target minus a certain amount of units (lets say 2)…

I came up with:

var dir = hit.transform.position - transform.position;
var enemypoint : Vector3 = hit.transform.position + dir.normalized * 2;

It gives the position minus 2 units but it’s always at a fixed place from the enemy (let’s say always on the left)

I need the position to be 2 units away from the enemy facing the player if you know what I mean… It’s hard to explain. This way I can make the player walk to the enemy position minus 2 units so the player is next to the enemy instead of IN the enemy.

Hope someone can help.

In my original code I used: GetComponent(GameController).playerObject.position;
Instead of transform.position because the code gets executed in my camera. (Posted like this to give a better understanding). I changed it to target.position and it all works now…

@robertbu Thanks for the debug tip!
@BoredMormon Thanks, it works as intended that way!

My final code looks like:

var dir = hit.transform.position - target.position;
var enemypoint : Vector3 = hit.transform.position - dir.normalized * 2;