Search code examples
c#unity-game-enginegeometry2dspawn

Object spawn on opposite side


please help with object spawning, i have a circle, outside the circle there is an object that rotates and follows the object inside the circle, the object should spawn on the opposite side in a straight line, i have depicted this in the picture, thanks for any help.

I know the coordinates of point A, I need to spawn point B. I could, knowing the radius, put a point? Then a line will be drawn between point A and B and a projectile will fly from point A to point B I apologize if I don’t convey my thoughts well, I don’t know English well.

I just need point B to spawn in a straight line from point A with a radius I know enter image description here

    step = 2 * Mathf.PI / count;
    Vector2 spawnPos = center + RotateVector(radius, step * randomIndex);
    Instantiate(scenePrefab, spawnPos, Quaternion.identity);


Solution

  • Found the solution here, maybe it will be useful for someone enter link description here

        Vector2 direction = gameObject.transform.position - player.transform.position;
                circleNormal = Vector2.Perpendicular(direction);
                Instantiate(endPoint, ComputeB(center, circleNormal, gameObject.transform.position, direction), Quaternion.identity);
     
    private Vector3 ComputeB( Vector3 circleCenter, Vector3 circleNormalDirection, Vector3 point, Vector3 direction )
        {
            float a = Vector3.SignedAngle( circleCenter - point, direction, circleNormalDirection );
            float w = 0;
            if ( a >= 0 ) w = 180 - 2 * a; // because w + a + a = 180;
            else w = -( 180 + 2 * a );
            Vector3 BO = Quaternion.AngleAxis(w, -circleNormalDirection) * (point - circleCenter);
            return circleCenter + BO;
        }