This is my piece of script
transform.DOMove((enemy.ClosestTarget().transform.position), .3f);
how could i substract a specific value from that “(enemy.ClosestTarget().transform.position)”.
Enemy is a reference to another script, and ClosestTarget is a method inside that script, closest target is a gameobject method and it returns the closest object.
In this case i want to take that position and substract a certain number, it works without but i’d like to move the player a little far away than the exact position.
In case anyone needs it:
public GameObject ClosestTarget()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag(enemyTag);
GameObject closest = null;
float distance = maxDistance;
float currAngle = maxAngle;
Vector3 position = transform.position;
// If there's already a current target and it's still within range, return it
if (currentTarget && (currentTarget.position - transform.position).magnitude < maxDistance)
{
return currentTarget.gameObject;
}
else
{
isTargeting = false;
}
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.magnitude;
if (curDistance < distance)
{
Vector3 viewPos = mainCamera.WorldToViewportPoint(go.transform.position);
Vector2 newPos = new Vector3(viewPos.x - 0.5f, viewPos.y - 0.5f);
if (Vector3.Angle(diff.normalized, mainCamera.transform.forward) < maxAngle)
{
closest = go;
currAngle = Vector3.Angle(diff.normalized, mainCamera.transform.forward.normalized);
distance = curDistance;
}
}
}
return closest;
}