Skip to main content
SOLVED
Source Link

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!

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!

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!

Source Link

Gap between two 3D objects in Unity

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!