How can I make the script that build walls to work in any scene not only on terrain ?

Could be in space or on terrain or on plane but now it’s working only on terrain and also if the terrain is at position 250,0,250 I need it to be working in any place not only on terrain and that the terrain need to be in specific position.

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

public class Walls : MonoBehaviour
{
    public GameObject prefab;
    public Vector3 wallsStartPosition;
    public float width = 0;
    public float height = 1;
    public float lengthX = 2;
    public float lengthZ = 2;
    public float time = 1;

    private List<GameObject> walls = new List<GameObject>();
    private IEnumerator coroutine;

    void Start()
    {       
        for (int i = -2; i < 2; i++)
        {
            GameObject go = Instantiate(prefab);
            go.transform.parent = transform;
            go.tag = "Wall";
            Vector3 scale = Vector3.one;
            Vector3 adjustedPosition = wallsStartPosition;

            float sign = Mathf.Sign(i);
            if ((i * sign) % 2 == 0)
            {
                adjustedPosition.x += (lengthX * sign) / 2;
                scale.x = width;
                scale.y = height;
                scale.z *= lengthZ + width;
            }
            else
            {
                adjustedPosition.z += (lengthZ * sign) / 2;
                scale.x *= lengthX + width;
                scale.y = height;
                scale.z = width;
            }
            adjustedPosition.y -= height / 2;
            go.transform.localScale = scale;
            go.transform.localPosition = adjustedPosition;
            walls.Add(go);
        }
        coroutine = raiseWall(walls[0], height, time, 0, 100);
        StartCoroutine(coroutine);
        coroutine = raiseWall(walls[2], height, time, 0, 100);
        StartCoroutine(coroutine);
        coroutine = raiseWall(walls[1], height, time, 0, 100);
        StartCoroutine(coroutine);
        coroutine = raiseWall(walls[3], height, time, 0, 100);
        StartCoroutine(coroutine);
    }

    IEnumerator raiseWall(GameObject wall, float amount, float time, float delay, float steps)
    {
        Vector3 position = wall.transform.localPosition;
        float finalHeight = position.y + amount;

        yield return new WaitForSeconds(delay);

        while (position.y < finalHeight)
        {
            float remainingHeight = finalHeight - position.y;
            position.y += Mathf.Min(remainingHeight, amount / steps);
            wall.transform.localPosition = position;
            yield return new WaitForSeconds(time / steps);
        }
        yield break;
    }
}

Steps to success here:

  • identify what it means “to build a wall”: what specific user input and feedback during the emplacement leads to what specific wall outline, feedback, scaling, interactions, etc.

  • identify the ways your current implementation relies on terrain (such as the collision? the height? The scale? what?)

  • abstract and/or refactor those dependencies so that either:

  1. you can trivially present the other things you want as “wall-placeable things”

OR

  1. you can do without the reliance on the things you presently get from using terrain.
1 Like