Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Object spawn on opposite side

Discussion in '2D' started by Lemur8063, Jun 9, 2023.

  1. Lemur8063

    Lemur8063

    Joined:
    Jan 27, 2015
    Posts:
    16
    Hello, 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.
    Снимок экрана 2023-06-10 004133.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    The other point is just the difference vector, but either negated or added in the opposite way.

    If that's not what you're asking, how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. Lemur8063

    Lemur8063

    Joined:
    Jan 27, 2015
    Posts:
    16
    I don't quite understand how to use it, the point inside the circle is almost (Player) never in the center and that's the problem. 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.
    Code (CSharp):
    1. step = 2 * Mathf.PI / count;
    2. Vector2 spawnPos = center + RotateVector(radius, step * randomIndex);
    3. Instantiate(scenePrefab, spawnPos, Quaternion.identity);
    Снимок экрана 2023-06-10 021655.png
     
  4. Lemur8063

    Lemur8063

    Joined:
    Jan 27, 2015
    Posts:
    16
    I just need point B to spawn in a straight line from point A with a radius I know
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    What you contemplate requires more known variables, such as the lengths of the line segments.

    This really sounds like an XYProblem because you haven't specified why there is so much missing data involved.

    What are you actually trying to solve here?
     
  6. Lemur8063

    Lemur8063

    Joined:
    Jan 27, 2015
    Posts:
    16
    Found the solution here, maybe it will be useful for someone
    https://answers.unity.com/questions/1657918/i-have-a-line-that-goes-through-a-circle-how-can-i.html

    Code (CSharp):
    1. Vector2 direction = gameObject.transform.position - player.transform.position;
    2.             circleNormal = Vector2.Perpendicular(direction);
    3.             Instantiate(endPoint, ComputeB(center, circleNormal, gameObject.transform.position, direction), Quaternion.identity);
    4.  
    5. private Vector3 ComputeB( Vector3 circleCenter, Vector3 circleNormalDirection, Vector3 point, Vector3 direction )
    6.     {
    7.         float a = Vector3.SignedAngle( circleCenter - point, direction, circleNormalDirection );
    8.         float w = 0;
    9.         if ( a >= 0 ) w = 180 - 2 * a; // because w + a + a = 180;
    10.         else w = -( 180 + 2 * a );
    11.         Vector3 BO = Quaternion.AngleAxis(w, -circleNormalDirection) * (point - circleCenter);
    12.         return circleCenter + BO;
    13.     }