0
\$\begingroup\$

Quick question related to Unity: if I have two objects (A and B) to create some tunnel, how can I make a constant gap? I thought of having A and B instantiated with a random value to start with; however, it will not be constant gap. So I was wondering what other ways are (as I'm reading now, there is Vector3.Distance method; but implementing it makes it a little bit more confusing). The idea is to have your player go through to this gap constant of object A and B.

Picture as an example:enter image description here However, the gaps have inconsistencies because they are not clean. Sometimes there are really close together and the red ball cannot even go through (like the next image)enter image description here

This is the code example that I have at this moment:

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

public class SpawnManager : MonoBehaviour
{
    public GameObject[] obstaclesPrefab;
    private float startDelay = 2;
    private float repeatRate = 2;
    private PlayerControl playerControllerScript;
    float gap = 4;
    // Start is called before the first frame update
    void Start()
    {
        playerControllerScript = GameObject.Find("Tomato").GetComponent<PlayerControl>();
        InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void SpawnObstacle()
    {
        int obstaclesIndex = Random.Range(0, obstaclesPrefab.Length);
        float randomY = Random.Range(-1.0f, 2.0f);
        float randomY2 = Random.Range(-1.0f, 2.0f);
        Vector3 pipe = new Vector3(25, randomY * gap, -10);
        Vector3 pipe2 = new Vector3(25, randomY2 * gap, -10);
        float distance = Vector3.Distance(pipe2, pipe);
        if (playerControllerScript.gameOver == false)
        {
            Instantiate(obstaclesPrefab[obstaclesIndex], pipe2, obstaclesPrefab[obstaclesIndex].transform.rotation);
            Instantiate(obstaclesPrefab[obstaclesIndex], pipe, obstaclesPrefab[obstaclesIndex].transform.rotation);
        }
    }
}

I'm open to suggestions as I'm getting started with game development and learning too. Thank you for time and have a great day!

EDIT: Thank you DMGregory for such a simple solution and help. I was just tired and my brain was mush. This totally did the trick!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Did you consider doing just one random roll for each pair of pipes, then adding a constant offset to get two positions from that single roll? \$\endgroup\$
    – DMGregory
    Commented Oct 16, 2022 at 17:33
  • \$\begingroup\$ Well, I guess I was just tired. Your thought process helped me a lot and I was able to fix it :). I appreciate it a lot! \$\endgroup\$ Commented Oct 16, 2022 at 18:34
  • 1
    \$\begingroup\$ If you've solved your problem, be sure to post your solution as an Answer below. This can help future developers who are trying to solve similar problems. \$\endgroup\$
    – DMGregory
    Commented Oct 16, 2022 at 18:36

1 Answer 1

0
\$\begingroup\$

Thank you DMGregory for such a simple solution and help. I was just tired and my brain was mush. This totally did the trick!

Code example:

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

public class SpawnManager : MonoBehaviour
{
    public GameObject[] obstaclesPrefab;
    private float startDelay = 2;
    private float repeatRate = 2;
    private PlayerControl playerControllerScript;
    float gap = 7.0f;
    // Start is called before the first frame update
    void Start()
    {
        playerControllerScript = GameObject.Find("Tomato").GetComponent<PlayerControl>();
        InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    void SpawnObstacle()
    {
        int obstaclesIndex = Random.Range(0, obstaclesPrefab.Length);
        float randomY = Random.Range(-1.5f, 2.5f);
        Vector3 randomHeight = new Vector3(25, randomY, -10); //CHANGED HERE
        Vector3 randomHeight2 = new Vector3(25, randomY + gap, -10); //CHANGED HERE (I'm adding gap, not multiplying it)
        float distance = Vector3.Distance(randomHeight, randomHeight2);
        if (playerControllerScript.gameOver == false)
        {
            Instantiate(obstaclesPrefab[obstaclesIndex], randomHeight, obstaclesPrefab[obstaclesIndex].transform.rotation);
            Instantiate(obstaclesPrefab[obstaclesIndex], randomHeight2, obstaclesPrefab[obstaclesIndex].transform.rotation);
        }
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Right now this answer just says "thank you" — it does not show what you changed to solve the problem, so it won't be very helpful to future readers trying to solve the same problem. Don't worry about gratitude — worry about clearly sharing usable game development solutions. \$\endgroup\$
    – DMGregory
    Commented Oct 17, 2022 at 11:10
  • \$\begingroup\$ Added, thank you :) \$\endgroup\$ Commented Oct 17, 2022 at 14:37
  • \$\begingroup\$ Code helps. Code plus an explanation of what you changed and why that solves the problem is even better. \$\endgroup\$
    – DMGregory
    Commented Oct 17, 2022 at 19:10
  • \$\begingroup\$ Don't forget to mark this answer as "Accepted" if it solved your problem, by clicking the checkmark icon in the top left. \$\endgroup\$
    – DMGregory
    Commented Oct 19, 2022 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .