1

I'm developing a video game in Unity and I have come across a vector mathematics problem designing one of the levels. I want to spawn two rectangular blocks attached to each other parallel to the diagonal of the screen: wo rectangular blocks attached to each other in tangent to the diagonal The red block needs to spawn relative to the position of the yellow block and the yellow block needs to spawn in a way that if I were to shift it back to the diagonal of the screen it would always end up in a position on the diagonal of the screen that falls within the confines of the screen for example somewhere on the red line in the second example:Dioganal line tangent to each position the yellow bar can have I want to do this in order too make sure that whenever I tell the yellow line and red line to move towards the centre at the same time that they will always end up along that line when they are at the centre of the screen Movement of the yellow and red bar. The yellow bar needs to always end up along the red line in the middle when moving towards the centre. I am trying to make sure that this happens for both diagonals of the screen as well as each of the four sides of the rectangle However for some reason my math doesn't seem to work. Whenever the bars spawn, they spawn all over the place:How they spawn now

This is the current code I have written:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;

public class BallSpawnerControl3 : MonoBehaviour
{
    int index = 1;
    public Transform[] spawnPointsLevel3;
    int randomSpawnPoint, Interact;
    ObjectPooler objectPooler;
    public static bool Spawn;
    public static int Level3Trans = 1;
    private float RandomSpawn;
    private float Y_intercept;
    float PointBarVectorLength;
    float PointBarLength;
    Quaternion rot;
    private Vector2 Ylength;
    private Vector2 Xlength;
    Vector2 PointBarVectorConv;
    Quaternion PointBarVerticalRot;
    Quaternion PointBarDiagonalLeftRot;
    Quaternion PointBarDiagonalRightRot;
    Quaternion RedBarVerticalRot;
    Quaternion RedBarDiagonalLeftRot;
    Quaternion RedBarDiagonalRightRot;
    Quaternion QuaResult;

    public void Start()
    {
        objectPooler = ObjectPooler.Instance;
        
        GameObject PointBar = (GameObject) Resources.Load("PointBar Horizontal");
        GameObject PointBarVertical = (GameObject) Resources.Load("PointBar Vertical");
        GameObject PointBarDiagonalLeft = (GameObject)Resources.Load("PointBar Diagonal Left");
        GameObject PointBarDiagonalRight = (GameObject)Resources.Load("PointBar Diagonal Right");
        GameObject RedBarVertical = (GameObject)Resources.Load("Red Bar Vertical");
        GameObject RedBarDiagonalLeft = (GameObject)Resources.Load("Red Bar Diagonal Left");
        GameObject RedBarDiagonalRight = (GameObject)Resources.Load("Red Bar Diagonal Right");
        PointBarVerticalRot = PointBarVertical.GetComponent<Transform>().rotation;
        PointBarDiagonalLeftRot = PointBarDiagonalLeft.GetComponent<Transform>().rotation;
        PointBarDiagonalRightRot = PointBarDiagonalRight.GetComponent<Transform>().rotation;
        RedBarVerticalRot = RedBarVertical.GetComponent<Transform>().rotation;
        RedBarDiagonalLeftRot = RedBarDiagonalLeft.GetComponent<Transform>().rotation;
        RedBarDiagonalRightRot = RedBarDiagonalRight.GetComponent<Transform>().rotation;
        PointBarVectorLength = PointBar.GetComponent<SpriteRenderer>().bounds.size.x;
        PointBarVectorConv = Camera.main.WorldToViewportPoint(new Vector2(PointBarVectorLength, 0f));

        rot = new Quaternion(0, 180, 0,0);
        Spawn = true;
       
        InvokeRepeating("SpawnAInteract", 0f, 1f);
    }

    void SpawnAInteract()
    {

        // Scene scene = SceneManager.GetActiveScene();


        if (Spawn && ScoreScript.scoreValue > 11 && SceneManager.GetActiveScene().name == "Level 3")
        {
            if (ScoreScript.scoreValue > 4 && Level3Trans == 1)
            {
                StartCoroutine(LevelTransition3());
            }

            else
            {
                randomSpawnPoint = UnityEngine.Random.Range(0, spawnPointsLevel3.Length);
                Interact = 1;

                if (randomSpawnPoint == 0 || randomSpawnPoint == 4)
                {
                    RandomSpawn = UnityEngine.Random.Range(-0.5f, 0.5f - PointBarVectorConv.x);

                    if (index % 5 != 0)
                    {
                        objectPooler.SpawnFromPool("PointBar Horizontal", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn, 0, 0)), Quaternion.identity);
                        Debug.Log("PointBar Horizontal");
                    }

                    QuaResult = transform.rotation * rot;

                    objectPooler.SpawnFromPool("Red Bar Horizontal", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn + PointBarVectorLength, 0, 0)), Quaternion.identity);
                    objectPooler.SpawnFromPool("Red Bar Horizontal", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn, 0, 0)), QuaResult);
                    Debug.Log("PointBar Diagonal Left");
                }

                else if (randomSpawnPoint == 1 || randomSpawnPoint == 5)
                {
                    if (randomSpawnPoint == 1)
                    {
                        RandomSpawn = UnityEngine.Random.Range(0.7f, 1.8f);
                        Y_intercept = 3.96f;

                    }

                    if (randomSpawnPoint == 5)
                    {
                        RandomSpawn = UnityEngine.Random.Range(-0.8f, 0.3f);
                        Y_intercept = -1.085f;
                    }


                    int Gradient = -1;

                    float PointBarYPosition = DiagonalRight(RandomSpawn, Y_intercept);

                    Vector2 PointBarPosition = Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn, PointBarYPosition, 0));

                    if (index % 5 != 0)
                    {
                        objectPooler.SpawnFromPool("PointBar Diagonal Left", PointBarPosition, PointBarDiagonalLeftRot);
                        Debug.Log("PointBar Diagonal Left");
                    }

                    QuaResult = RedBarDiagonalLeftRot * rot;

                    objectPooler.SpawnFromPool("Red Bar Diagonal Left", Camera.main.ViewportToWorldPoint(new Vector3(RedBarDiagonalRightX(RandomSpawn, PointBarVectorLength, Gradient), RedBarDiagonalRightY(PointBarVectorLength, PointBarYPosition, Gradient), 0)), RedBarDiagonalLeftRot);//Adapt this and the next line in order to spawn to red bars attached to the yellow one
                    objectPooler.SpawnFromPool("Red Bar Diagonal Left", PointBarPosition, QuaResult);
                    Debug.Log("Red Bar Diagonal Left");
                }

                else if (randomSpawnPoint == 2 || randomSpawnPoint == 6) // |□|
                {
                    RandomSpawn = UnityEngine.Random.Range(-0.5f, 0.5f - PointBarVectorConv.x);

                    if (index % 5 != 0)
                    {
                        objectPooler.SpawnFromPool("PointBar Vertical", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(0, RandomSpawn, 0)), PointBarVerticalRot);
                        Debug.Log("Pointbar Vertical");
                    }

                    QuaResult = RedBarVerticalRot * rot;

                    objectPooler.SpawnFromPool("Red Bar Vertical", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(0, RandomSpawn + PointBarVectorLength, 0)), RedBarVerticalRot);
                    objectPooler.SpawnFromPool("Red Bar Vertical", spawnPointsLevel3[randomSpawnPoint].position + Camera.main.ViewportToWorldPoint(new Vector3(0, RandomSpawn, 0)), QuaResult);
                    Debug.Log("Red Bar Vertical");
                }

                else if (randomSpawnPoint == 3 || randomSpawnPoint == 7)  // /□/
                {
                    if (randomSpawnPoint == 3)
                    {
                        RandomSpawn = UnityEngine.Random.Range(0.7f, 1.8f);
                        Y_intercept = -2.96f;
                    }

                    if (randomSpawnPoint == 7)
                    {
                        RandomSpawn = UnityEngine.Random.Range(-0.8f, 0.3f);
                        Y_intercept = 2.085f;
                    }

                    int Gradient = 1;

                    float PointBarYPosition = DiagonalLeft(RandomSpawn, Y_intercept);

                    Vector2 PointBarPosition = Camera.main.ViewportToWorldPoint(new Vector3(RandomSpawn, PointBarYPosition, 0));// main might have to be changed too the camera active in Level 3

                    if (index % 5 != 0)
                    {
                        objectPooler.SpawnFromPool("PointBar Diagonal Right", PointBarPosition, PointBarDiagonalRightRot);
                        Debug.Log("PointBar Diagonal Right");
                    }

                    QuaResult = RedBarDiagonalRightRot * rot;

                    objectPooler.SpawnFromPool("Red Bar Diagonal Right", Camera.main.ViewportToWorldPoint(new Vector3(RedBarDiagonalRightX(RandomSpawn, PointBarVectorLength, Gradient), RedBarDiagonalRightY(PointBarVectorLength, PointBarYPosition, Gradient), 0)), RedBarDiagonalRightRot);// Adapt this and the next line in order to spawn to red bars attached to the yellow one
                    objectPooler.SpawnFromPool("Red Bar Diagonal Right", PointBarPosition, QuaResult);
                    Debug.Log("Red Bar Diagonal Right");
                }


                index++;

            }

        }

    }

    private float DiagonalRight(float RandomSpawn, float Y_intercept) // Y = -1X + 14.09 ==> Formula
    {
        float Yposition = -RandomSpawn + Y_intercept;
        return Yposition;

    }

    private float RedBarDiagonalRightX(float RandomSpawn, float PointBarVectorLength, int Gradient) // Y = -1X + 14.09 ==> Formula
    {
        float RedXPosition = RandomSpawn + PointBarVectorLength / (float)(Math.Sqrt(1 + Math.Pow(Gradient, 2)));
        return RedXPosition;

    }

    private float RedBarDiagonalRightY(float PointBarVectorLength, float PointBarYPosition, int Gradient) // Y = -1X + 14.09 ==> Formula
    {
        float RedYPosition = PointBarYPosition + (Gradient * PointBarVectorLength) / (float)(Math.Sqrt(1 + Math.Pow(Gradient, 2)));
        return RedYPosition;

    }

    
    private float DiagonalLeft(float RandomSpawn, float Y_intercept) // Y = -1X + 14.09 ==> Formula
    {
        float Yposition = RandomSpawn - Y_intercept;
        return Yposition;

    }

  

    private IEnumerator LevelTransition3()
    {

        yield return new WaitForSeconds(1f);
        Level3Trans++;
        Debug.Log("Level Trans :" + Level3Trans);

    }


}

I wanna note that

1)randomSpawnPoint == Spawnpoint (1+ randomSpawnPoint) for the positions shown in the picture shown here:positions of Spawnpoints

  1. I calculated most of the float values manually by comparing the size of the screen to the size of my objects within the scene.

My question now is to anyone experienced in Unity if there isn't an easy way of doing what I'm trying to achieve that I don't seem to be grasping. Because given all the games that have been created over the years it seems like I'm doing a lot of calculations for something that I thing should be relatively easy to do. Or if my calculations are wrong in an obvious way if someone could help me in the right direction I would appreciate it allot?

Maurice Bekambo
  • 325
  • 6
  • 21

0 Answers0