I'm attempting to place props randomly on a mesh. The mesh has 2 materials and I'd like to only place the props on one of the materials. I'm currently doing this through raycasting, determining the material at the point hit and if the material is correct I place a prop at that point. I'm having an issue where the material is being determined incorrectly and I'm not certain why. Code is below.
using UnityEngine;
public class BoundingBoxTry : MonoBehaviour {
// Use this for initialization
public MeshFilter meshFilter;
private Renderer rend;
private Mesh mesh;
private Bounds bnds;
private float nextX, nextZ, maxX, minX, maxZ, minZ;
public GameObject rayOrigin;
public GameObject propPreFab;
private MeshCollider thisCollider;
void Start () {
thisCollider = GetComponentInChildren<MeshCollider>();
rend = GetComponentInChildren<Renderer>();
mesh = GetComponentInChildren<MeshFilter>().sharedMesh;
bnds = rend.bounds;
}
void Update () {
nextX = Random.Range(0, bnds.size.x);
nextZ = Random.Range(0, bnds.size.z);
nextX = nextX-bnds.size.x/2;
nextZ = nextZ-bnds.size.z/2;
nextX = bnds.center.x-nextX;
nextZ = bnds.center.z-nextZ;
RaycastHit hit = new RaycastHit();
Vector3 randomCoord = new Vector3(nextX, 0, nextZ);
rayOrigin.transform.position = new Vector3(nextX, 100, nextZ);
bool didHit = Physics.Raycast(rayOrigin.transform.position, -new Vector3(0, 1, 0), out hit);
int vertCounter = 0;
if(didHit && hit.collider == thisCollider){
for (int sMC = 0; sMC < mesh.subMeshCount; sMC++)
{
int[] subMesh = mesh.GetTriangles(sMC);
vertCounter = vertCounter + subMesh.Length;
if(hit.triangleIndex < vertCounter){
if(rend.sharedMaterials[sMC] == rayOrigin.GetComponent<Renderer>().sharedMaterial){
GameObject prop = Instantiate(propPreFab, hit.point, Quaternion.identity);
}
else{
}
}
}
}
}
}
I've also attached a picture to demonstrate the behavior I'm seeing.
Of course, as soon as I ask and sleep on the problem I'm able to figure it out. Arman's link was helpful as well. The issue I was seeing was caused by my not fully understanding the data I was getting from the triangleIndex call.
When calling triangleIndex you receive a vertex index back. To get the triangle hit from that vertex you must multiply the vertex index by 3 and then validate which subMesh was struck by the raycast.
Related
So the idea is to have a plane and grid placement system in augmented reality with the ability to place and move characters on grids. I already have an example for a mobile device, I have a script that generates grid and a script that allows me to place objects and it works just fine, however, I can't figure out how to use all of the above and if it's possible in AR. For example, I want to detect a plane then instantiate a level and put some objects on it.
Here's the script that is attached to the GridManager and is used to make a grid:
[SerializeField] private float size = 0.05f;
public Vector3 GetNearestPointOnGrid(Vector3 position)
{
position -= transform.position;
int xCount = Mathf.RoundToInt(position.x / size);
int yCount = Mathf.RoundToInt(position.y / size);
int zCount = Mathf.RoundToInt(position.z / size);
Vector3 result = new Vector3(
(float)xCount * size,
(float)yCount * size,
(float)zCount * size);
result += transform.position;
return result;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
for (float x = 0; x < 40; x += size)
{
for (float z = 0; z < 40; z += size)
{
var point = GetNearestPointOnGrid(new Vector3(x, 0f, z));
Gizmos.DrawSphere(point, 0.01f);
}
}
}
and here's the one that's attached to the PlacerManager and used to place objects on the grid:
private Grid grid;
private void Awake()
{
grid = FindObjectOfType<Grid>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
{
PlaceCubeNear(hitInfo.point);
}
}
}
private void PlaceCubeNear(Vector3 clickPoint)
{
var finalPosition = grid.GetNearestPointOnGrid(clickPoint);
GameObject.CreatePrimitive(PrimitiveType.Cube).transform.position = finalPosition;
}
You can use the raycast options to identify different objects
Raycasting and/or colliders are the way to go.
The sample scene in AR Foundation has a script called PlaceOnPlane.cs that shows how you can detect when a user touches the screen. E.g:
if (Input.touchCount == 1) {
if (m_RaycastManager.Raycast(Input.GetTouch(0).position, s_Hits, TrackableType.PlaneWithinPolygon))
{
// Raycast hits are sorted by distance, so the first one
// will be the closest hit.
var hitPose = s_Hits[0].pose;
if (spawnedObject == null)
{
spawnedObject = Instantiate(m_PlacedPrefab, hitPose.position, hitPose.rotation);
}
}
}
This allows you to get the screen touch position and then raycast from it to the real world scene. In this example, a game object is instantiated in that location. For your case, you can instantiated a level if your hit a plane or a plane exist around the hit position.
I am trying to create a Fruit Ninja style game on Unity 2D and I want to create a trail that follows where the player has "cut". I've tried to instantiate a "cut" object that contains the line renderer every time a user drags. However, the line renderer is not showing up. Can anyone correct any errors or suggest a new method?
public class CreateCuts : MonoBehaviour
{
public GameObject cut;
public float cutDestroyTime;
private bool dragging = false;
private Vector2 swipeStart;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
dragging = true;
swipeStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
else if (Input.GetMouseButtonUp(0) && dragging)
{
createCut();
}
}
private void createCut()
{
this.dragging = false;
Vector2 swipeEnd = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject cut = Instantiate(this.cut, this.swipeStart, Quaternion.identity) as GameObject;
cut.GetComponent<LineRenderer>().positionCount = 1 ;
cut.GetComponent<LineRenderer>().enabled = true;
cut.GetComponent<LineRenderer>().SetPosition(0, this.swipeStart);
cut.GetComponent<LineRenderer>().SetPosition(1, swipeEnd);
Vector2[] colliderPoints = new Vector2[2];
colliderPoints[0] = new Vector2(0.0f, 0.0f);
colliderPoints[1] = swipeEnd - swipeStart;
cut.GetComponent<EdgeCollider2D>().points = colliderPoints;
Destroy(cut.gameObject, this.cutDestroyTime);
}
}
I expect there to be a line, but nothing shows up. There is also a warning stating that the SetPosition(1, swipeEnd) is out of bounds.
EDIT: Here are the settings of my cut object
1st part of cut object settings
2nd part of cut object settings
Positions tab of line renderer
I want to create a trail that follows where the player has "cut".
The word "trail" indicates that you should rather use a trail renderer!
Manual: https://docs.unity3d.com/Manual/class-TrailRenderer.html
API reference: https://docs.unity3d.com/ScriptReference/TrailRenderer.html
Back to your original question:
Your linerenderer probably is rendered but at a random position, because of Vector2 to Vector3 conversion, i dunno your project structure but this can be the case.
Please post a picture with one of your cut gameobject, that holds your linerenderer, and also extend the positions tab on the linerenderer so we can see your points xyz coordinates
Also apply the changes mentioned by commenters, because you really need 2 verticies for a line :P
Update:
public class CreateCuts : MonoBehaviour
{
public GameObject cut;
public float cutDestroyTime;
private bool dragging = false;
private Vector3 swipeStart;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
dragging = true;
swipeStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log("Swipe Start: " + swipeStart);
}
else if (Input.GetMouseButtonUp(0) && dragging)
{
createCut();
}
}
private void createCut()
{
this.dragging = false;
Vector3 swipeEnd = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log("SwipeEnd: " + swipeEnd);
GameObject cut = Instantiate(this.cut, swipeStart, Quaternion.identity);
cut.GetComponent<LineRenderer>().positionCount = 2;
// why is it not enabled by default if you just instantiate the gameobject O.o?
cut.GetComponent<LineRenderer>().enabled = true;
cut.GetComponent<LineRenderer>().SetPositions(new Vector3[]{
new Vector3(swipeStart.x, swipeStart.y, 10),
new Vector3(swipeEnd.x, swipeEnd.y, 10)
// z is zero cos we are in 2d in unity up axis is Y we set it to 10 for visibility reasons tho}
});
// Commented out cos atm we are "debugging" your linerenderer
// Vector2[] colliderPoints = new Vector2[2];
// colliderPoints[0] = new Vector2(0.0f, 0.0f);
// colliderPoints[1] = swipeEnd - swipeStart;
// cut.GetComponent<EdgeCollider2D>().points = colliderPoints;
//Destroy(cut.gameObject, this.cutDestroyTime);
}
}
I want the player to draw a line through the mouse, and this line has a collider, but this collider appears in the wrong place.
I have tried these methods with ScreenToWorldPoint, ScreenToViewportPoint and ScreenPointToRay, but these results are not what I want.
public class DrawLine : MonoBehaviour
{
public GameObject linePrefab;
public GameObject currentLine;
public LineRenderer lineRenderer;
public EdgeCollider2D edgeCollider;
public List<Vector2> fingerPositions;
public List<Vector2> fingerWorldPositions;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
CreateLine();
}
if (Input.GetMouseButton(0))
{
Vector2 tempFingerPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Vector2.Distance(tempFingerPos, fingerPositions[fingerPositions.Count - 1]) > .1f)
{
UpdateLine(tempFingerPos);
}
}
}
void CreateLine()
{
currentLine = Instantiate(linePrefab, Vector2.zero, Quaternion.identity);
lineRenderer = currentLine.GetComponent<LineRenderer>();
edgeCollider = currentLine.GetComponent<EdgeCollider2D>();
fingerPositions.Clear();
Vector3 tempMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
fingerPositions.Add(tempMousePosition);
fingerPositions.Add(tempMousePosition);
lineRenderer.SetPosition(0, fingerPositions[0]);
lineRenderer.SetPosition(1, fingerPositions[1]);
edgeCollider.points = fingerPositions.ToArray();
}
void UpdateLine(Vector2 newFingerPos)
{
fingerPositions.Add(newFingerPos);
lineRenderer.positionCount++;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, newFingerPos);
edgeCollider.points = fingerPositions.ToArray();
}
}
The black line is drawn with a mouse and the green line is Edge collider 2D.
I had your exact same problem some time ago in a project of mine. I fixed it by adjusting the transform.position of the linerenderer object to the location of the next point. And after setting the points, i would reset the position of the gameobject to zero (last line of code). This worked for me, because my gameobject should always be at absolute zero. But if yours doesn't, then just change it to use relative position and child it to an gameobject if it isnt already.
Here is my code, I hope it helps you.
public GameObject LineDrawPrefab;
private LineRenderer lineRenderer;
private EdgeCollider2D edgeCollider;
private List<Vector2> edgePoints;
private GameObject activeLineGameObject;
private List<GameObject> lineGameObjects;
private void StartLine(Vector3 start, Color color)
{
var targetPos = new Vector3 { x = start.x, y = start.y, z = 0.1f };
drawing = true;
activeLineGameObject = Instantiate(LineDrawPrefab, Vector3.zero, Quaternion.identity);
lineGameObjects.Add(activeLineGameObject);
lineRenderer = activeLineGameObject.GetComponent<LineRenderer>();
edgeCollider = activeLineGameObject.GetComponent<EdgeCollider2D>();
lineRenderer.sortingOrder = 1;
lineRenderer.material = LineRendererMaterial;
lineRenderer.startColor = color;
lineRenderer.endColor = color;
lineRenderer.startWidth = 0.3f;
lineRenderer.endWidth = 0.3f;
lineRenderer.SetPosition(0, targetPos);
lineRenderer.SetPosition(1, targetPos);
edgePoints.Clear();
edgePoints.Add(targetPos);
edgeCollider.points = edgePoints.ToArray();
}
private void ExtendLine(Vector3 nextpoint)
{
if (lineRenderer == null))
{
StartLine(Camera.main.ScreenToWorldPoint(Input.mousePosition), color);
return;
}
var targetPos = new Vector3 { x = nextpoint.x, y = nextpoint.y, z = 0.1f };
lineRenderer.positionCount += 1;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, lineRenderer.transform.position = targetPos); // manipulate position to avoid the collider from spawning out of the object.
edgePoints.Add(nextpoint);
edgeCollider.points = edgePoints.ToArray();
activeLineGameObject.transform.position = Vector3.zero; // reset position
}
Edit: An additional tip. I (and you) used Camera.main here. This is not performant because it does an expensive lookup every time it is called.
(GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>())
I suggest to cache it in start or awake and then reuse that variable. This can potentially be a huge performance gain.
private Camera mainCamera;
private void Start()
{
mainCamera = Camera.main;
}
I'm trying out some procedural mesh extrusion but I've got two problems at the moment. The code that performs the extrusion is a part of a Unity example package, and is available here (MeshExtrusion.cs)
First problem is that the update to the mesh only shows when an additional point is added to the mesh, the GIF below should highlight this.
Second problem is that the mesh is twisting. I'm guessing this is due to the wrong vertices being extruded, but I'm not 100% sure. Here is my code that is implementing the mesh extrusion:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExtrudeTest : MonoBehaviour {
private Vector3 currentMousePosition;
private float currentMouseDistance;
MeshExtrusion.Edge[] preComputedEdges;
private Mesh srcMesh;
private List<Matrix4x4> extrusionPoints = new List<Matrix4x4>();
private Matrix4x4 currentTransformMatrix;
GameObject lineObject;
LineRenderer drawLine;
private bool invertFaces = false;
// Use this for initialization
void Start () {
srcMesh = GetComponent<MeshFilter>().sharedMesh;
preComputedEdges = MeshExtrusion.BuildManifoldEdges(srcMesh);
extrusionPoints.Add(transform.worldToLocalMatrix * Matrix4x4.TRS(transform.position, Quaternion.identity, Vector3.one));
lineObject = new GameObject("lineRenderer");
drawLine = lineObject.AddComponent<LineRenderer>();
}
//Store the current world mouse position.
public void storeMouseLocation()
{
Ray ray = Camera.main.ScreenPointToRay(UnityEngine.Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
currentMousePosition = hit.point;
currentMouseDistance = hit.distance;
currentTransformMatrix = hit.transform.localToWorldMatrix;
}
}
private void adjustMesh()
{
Matrix4x4 worldToLocal = transform.worldToLocalMatrix;
Matrix4x4[] finalSections = new Matrix4x4[extrusionPoints.Count];
Quaternion rotation;
Quaternion previousRotation = Quaternion.identity;
Vector3 direction;
for (int i=0; i < extrusionPoints.Count; i++)
{
if (i == 0)
{
direction = extrusionPoints[0].GetColumn(3) - extrusionPoints[1].GetColumn(3);
rotation = Quaternion.LookRotation(direction, Vector3.up);
previousRotation = rotation;
finalSections[i] = worldToLocal * Matrix4x4.TRS(transform.position, rotation, Vector3.one);
}
else if (i != extrusionPoints.Count - 1)
{
direction = extrusionPoints[i].GetColumn(3) - extrusionPoints[i + 1].GetColumn(3);
rotation = Quaternion.LookRotation(direction, Vector3.up);
// When the angle of the rotation compared to the last segment is too high
// smooth the rotation a little bit. Optimally we would smooth the entire sections array.
if (Quaternion.Angle(previousRotation, rotation) > 20)
{
rotation = Quaternion.Slerp(previousRotation, rotation, 0.5f);
}
previousRotation = rotation;
finalSections[i] = worldToLocal * Matrix4x4.TRS(extrusionPoints[i].GetColumn(3), rotation, Vector3.one);
}
else
{
finalSections[i] = finalSections[i - 1];
}
}
extrudeMesh(finalSections);
}
private void extrudeMesh(Matrix4x4[] sections)
{
Debug.Log("Extruding mesh");
MeshExtrusion.ExtrudeMesh(srcMesh, GetComponent<MeshFilter>().mesh, sections, preComputedEdges, invertFaces);
}
// Update is called once per frame
void Update () {
storeMouseLocation();
drawLine.SetPosition(0, extrusionPoints[extrusionPoints.Count-1].GetColumn(3));
drawLine.SetPosition(1, currentMousePosition);
if (Input.GetMouseButtonDown(0))
{
extrusionPoints.Add(transform.worldToLocalMatrix * Matrix4x4.TRS(transform.position + new Vector3(currentMousePosition.x, currentMousePosition.y, currentMousePosition.z),
Quaternion.identity,
Vector3.one));
}
if (extrusionPoints.Count >=2)
{
adjustMesh();
}
}
}
Here is a GIF that shows how it's currently performing:
https://puu.sh/xeS7e/bfc3d71fba.gif
I'm not entirely sure what is causing these problems, so if anyone could offer any advice or input, I'd greatly appreciate it. Thanks
The idea is to make that when the player is walking to the edge of the terrain he will stop wont be able to continue and fall.
And in my case i want the objects that move forward when they collide with the invisible wall the object will turn lerp back and move to the other side of the invisible walls.
Another problem that might come up later i read about is that if the objects moving too fast to the invisible walls there is a bug that let them move through ? Not sure about it.
This is a screenshot showing the invisible walls. I created a box collider set the Is Trigger to be on and set the 500 600 500 same as the terrain size.
This is the script of the Invisible Walls: The script i attached it to the Terrain:
using UnityEngine;
using System.Collections;
public class InvisibleWalls : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerExit(Collider other)
{
}
}
This is the script that create the space ships clone of them and make them move forward. But when they get to the edge of the terrain they just gone out. And i want them to lkerp/turn back to the other side.
This script is attached to the Spheres GameObject:
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
public class SphereBuilder : MonoBehaviour
{
public GameObject SpaceShip;
GameObject[] spheres;
public float moveSpeed = 50;
// for tracking properties change
private Vector3 _extents;
private int _sphereCount;
private float _sphereSize;
/// <summary>
/// How far to place spheres randomly.
/// </summary>
public Vector3 Extents;
/// <summary>
/// How many spheres wanted.
/// </summary>
public int SphereCount;
public float SphereSize;
private void Start()
{
spheres = GameObject.FindGameObjectsWithTag("MySphere");
}
private void OnValidate()
{
// prevent wrong values to be entered
Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
SphereCount = Mathf.Max(0, SphereCount);
SphereSize = Mathf.Max(0.0f, SphereSize);
}
private void Reset()
{
Extents = new Vector3(250.0f, 20.0f, 250.0f);
SphereCount = 100;
SphereSize = 20.0f;
}
private void Update()
{
UpdateSpheres();
MoveShips ();
}
private void MoveShips()
{
foreach (Transform child in spheres[0].transform)
{
child.transform.position += Vector3.forward * Time.deltaTime * moveSpeed;
}
}
private void UpdateSpheres()
{
if (Extents == _extents && SphereCount == _sphereCount && Mathf.Approximately(SphereSize, _sphereSize))
return;
// cleanup
var spheres = GameObject.FindGameObjectsWithTag("Sphere");
foreach (var t in spheres)
{
if (Application.isEditor)
{
DestroyImmediate(t);
}
else
{
Destroy(t);
}
}
var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
throw new InvalidOperationException("Terrain not found");
for (var i = 0; i < SphereCount; i++)
{
var o = Instantiate(SpaceShip);
o.tag = "Sphere";
o.transform.SetParent(gameObject.transform);
o.transform.localScale = new Vector3(SphereSize, SphereSize, SphereSize);
// get random position
var x = Random.Range(-Extents.x, Extents.x);
var y = Extents.y; // sphere altitude relative to terrain below
var z = Random.Range(-Extents.z, Extents.z);
// now send a ray down terrain to adjust Y according terrain below
var height = 10000.0f; // should be higher than highest terrain altitude
var origin = new Vector3(x, height, z);
var ray = new Ray(origin, Vector3.down);
RaycastHit hit;
var maxDistance = 20000.0f;
var nameToLayer = LayerMask.NameToLayer("Terrain");
var layerMask = 1 << nameToLayer;
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
{
var distance = hit.distance;
y = height - distance + y; // adjust
}
else
{
Debug.LogWarning("Terrain not hit, using default height !");
}
// place !
o.transform.position = new Vector3(x, y, z);
}
_extents = Extents;
_sphereCount = SphereCount;
_sphereSize = SphereSize;
}
}
And this is a small short video clip showing what happen to the space ships when they get to the terrain edge:
Spaceships video clip
Update what i did so far:
In top of script added:
public Terrain terrain;
private Vector3 boundLower;
private Vector3 boundUpper;
In Start function i added:
private void Start()
{
spheres = GameObject.FindGameObjectsWithTag("MySphere");
var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
throw new InvalidOperationException("Terrain not found");
boundLower = terrain.transform.position - terrain.transform.size / 2;
boundUpper = terrain.transform.position + terrain.transform.size / 2;
}
But getting errors on both lines: size property not exist:
boundLower = terrain.transform.position - terrain.transform.size / 2;
boundUpper = terrain.transform.position + terrain.transform.size / 2;
And changed the MoveShips function to this:
private Vector3 direction = Vector3.forward;
private void MoveShips() {
foreach (var child in spheres) {
var pos = child.transform.position + direction * Time.deltaTime * moveSpeed;
pos.x = Mathf.Clamp(pos.x, boundLower.x, boundUpper.x);
pos.z = Mathf.Clamp(pos.z, boundLower.z, boundUpper.z);
if (pos.x == boundLower.x || pos.x == boundUpper.x) direction.x = - direction.x;
if (pos.z == boundLower.z || pos.z == boundUpper.z) direction.z = - direction.z;
child.transform.position = pos;
}
}
I would suggest modifying MoveShips() changing Vector3.forward to a variable and flipping it when bounds are reached:
private Vector3 direction = Vector3.forward;
private void MoveShips() {
foreach (var child in spheres) {
var pos = child.transform.position + direction * Time.deltaTime * moveSpeed;
pos.x = Mathf.Clamp(pos.x, boundLower.x, boundUpper.x);
pos.z = Mathf.Clamp(pos.z, boundLower.z, boundUpper.z);
if (pos.x == boundLower.x || pos.x == boundUpper.x) direction.x = - direction.x;
if (pos.z == boundLower.z || pos.z == boundUpper.z) direction.z = - direction.z;
child.transform.position = pos;
}
}
This will remove unnecessary dependence on object collision engine for such a simple thing. Note, how this is making all ships to change direction when furthest reaches the bound. If you want them to move separately, you will need to move this logic to a separate script and attach it to a ship prefab.
And the bounds (boundLower and boundUpper) can be set either as script variables in editor or calculated from terrain, like:
boundLower = terrain.transform.position - terrain.TerrainData.size / 2;
boundUpper = terrain.transform.position + terrain.TerrainData.size / 2;
I would also suggest to move this:
var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
throw new InvalidOperationException("Terrain not found");
out of Update() into Start() unless you do something really funky with it in the process.
Lets start working though your problems one by one :
Question : The objects do not collide, why?
Answer : Objects do not collide from with-in the collider, only from the outside.
What you need in your case is 4 box collider, one at each edge of the map
Question : Another problem that might come up later i read about is that if the objects moving too fast to the invisible walls there is a bug that let them move through ? Not sure about it.
This is only a problem with objects moving at bullet-like speeds, you could edit the rigidbody to have detection mode : "continuous" or continuous-dynamic which will avoid this issue
The scripts, i do not think you would need them in this case, your original idea was good, just the implementation with a single collider over the whole terrain, instead of 4 seperate "wall" colliders, was the issue. As for the rest of the logic, i did not try decipher through that, thus i can not comment on it.