- Home /
A problem with arrays
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class GridController : MonoBehaviour {
// Get closest free point in grid
public Vector3 snapPoint (GameObject GroundObject, Vector3 hit){
float worldX = 2F;
float worldY = 2F;
float worldZ = 2F;
bool IsGenerated = false;
float snap = 0.1F;
List<Vector3> possiblePositionsList = new List<Vector3>();
List<Vector3> tempPositionsList = new List<Vector3>();
List<float> distanceList = new List<float>();
int minIndex; // Shortest distance
float minVal;
this.transform.position = hit;
Vector3 position = this.transform.position;
position.x = Mathf.Round(position.x / snap) * snap;
position.y = Mathf.Round(position.y / snap) * snap;
position.z = Mathf.Round(position.z / snap) * snap;
this.transform.position = position;
Vector3 gridStart = transform.position - new Vector3(worldX / 2, worldY / 2, worldZ / 2);
for(float x =0F; x<worldX; x += snap) {
for(float y =0F; y<worldY; y += snap) {
for(float z =0F; z<worldZ; z += snap) {
Vector3 tempPosition;
tempPosition = new Vector3(gridStart.x + x, gridStart.y + y, gridStart.z + z);
tempPositionsList.Add(tempPosition);
}
}
}
IsGenerated = true;
foreach (Vector3 tempPosition in tempPositionsList){
GroundObject.transform.position = tempPosition;
bool isFree = true;
// Check is object free space WIP
Collider[] hitColliders = Physics.OverlapSphere(GroundObject.transform.position, GroundObject.collider.bounds.extents.magnitude);
foreach(Collider currentCollider in hitColliders){
if(GroundObject.collider.bounds.Intersects(currentCollider.bounds)){
int adjustedTriangleCount = GroundObject.transform.GetComponent<MeshFilter>().mesh.triangles.Length / 3;
int otherAdjustedTriangleCount = currentCollider.transform.GetComponent<MeshFilter>().mesh.triangles.Length / 3;
for(int i = 0; i <= otherAdjustedTriangleCount; i ++){ // For every triangle in GroundObject...
for(int ib = 0; ib <= adjustedTriangleCount; ib ++){ // For every triangle in other object...
int[] aPoints = new int[3];
aPoints = new int[3] {GroundObject.GetComponent<MeshFilter>().mesh.triangles[(i - 1)*3 + 1], GroundObject.GetComponent<MeshFilter>().mesh.triangles[(i - 1)*3 + 2], GroundObject.GetComponent<MeshFilter>().mesh.triangles[(i - 1)*3 + 3]};
Vector3 a1 = GroundObject.transform.TransformPoint(GroundObject.GetComponent<MeshFilter>().mesh.vertices[aPoints[1]]);
Vector3 a2 = GroundObject.transform.TransformPoint(GroundObject.GetComponent<MeshFilter>().mesh.vertices[aPoints[2]]);
Vector3 a3 = GroundObject.transform.TransformPoint(GroundObject.GetComponent<MeshFilter>().mesh.vertices[aPoints[3]]);
int[] bPoints = new int[3];
bPoints = new int[3] {currentCollider.transform.GetComponent<MeshFilter>().mesh.triangles[(ib - 1)*3 + 1], currentCollider.transform.GetComponent<MeshFilter>().mesh.triangles[(ib - 1)*3 + 2], currentCollider.transform.GetComponent<MeshFilter>().mesh.triangles[(ib - 1)*3 + 3]};
Vector3 b1 = currentCollider.transform.TransformPoint(currentCollider.transform.gameObject.GetComponent<MeshFilter>().mesh.vertices[aPoints[1]]);
Vector3 b2 = currentCollider.transform.TransformPoint(currentCollider.transform.gameObject.GetComponent<MeshFilter>().mesh.vertices[aPoints[2]]);
Vector3 b3 = currentCollider.transform.TransformPoint(currentCollider.transform.gameObject.GetComponent<MeshFilter>().mesh.vertices[aPoints[3]]);
if(TriTriOverlap.TriTriIntersect(a1, a2, a3, b1, b2, b3)){
isFree = false;
}
}
}
}
}
if(isFree != false){
possiblePositionsList.Add (tempPosition);
Debug.Log ("It is free!");
}
}
foreach (Vector3 possiblePosition in possiblePositionsList){
distanceList.Add(Vector3.Distance(possiblePosition, transform.position));
}
minVal = distanceList.Min();
minIndex = distanceList.IndexOf(minVal);
return possiblePositionsList[minIndex];
}
}
What's wrong with aPoints and bPoints? Assets/Game_scripts/GridController.cs(52,57): error CS0266: Cannot implicitly convert type System.Collections.Generic.IEnumerable' to
int[]'. An explicit conversion exists (are you missing a cast?) Updated code.
EDIT: Updated code. IndexOutOfRangeException: Array index is out of range. GridController.snapPoint (UnityEngine.GameObject GroundObject, Vector3 hit) (at Assets/Game_scripts/GridController.cs:53) BuildScript.FixedUpdate () (at Assets/Game_scripts/BuildScript.cs:70)
Answer by pako · Jan 04, 2015 at 02:34 PM
In lines 13 and 18 don't use the square brackets, just the variable name:
aPoints = GroundObject.GetComponent<MeshFilter>().mesh.vertices.Skip(i - 1 * 3).Take(3);
bPoints = GroundObject.GetComponent<MeshFilter>().mesh.vertices.Skip(i - 1 * 3).Take(3);
By the way, I really don't understand where you find the "Skip" and "Take" functions/properties in:
mesh.vertices.Skip(i - 1 * 3).Take(3);
They are not part of Vecotr3, and mesh.vertices returns a Vector3 array. I guess you have implemented these yourself.
Actually, I just realized it does, if you import System.Linq. I don't use Linq much, and whenever I used it in the past I had never had any use for Take and Skip. Learn a new thing every day! Thanks for pointing it out!
So I need to skip i - 1 * 3 elements in an array and get three elements after them.
Elements that I want are i - 1 x 3 + 1, i - 1 x 3 + 2, i - 1 x 3 + 3.
You should use parentheses.
The way you have it, is like saying: i - (1 x 3), i.e. the multiplication precedes the addition. e.g if i = 5, it will equal to 5 - 3 = 2.
But you want (i - 1) x 3, so use parentheses like these. In this case, e.g if i = 5, it will equal to 4 x 3 = 12.
So, if you skip these, then Take(3), the first 3 elements of what's left will be returned and that's what you want.
Your answer
Follow this Question
Related Questions
Picking a Random Order for Levers 1 Answer
A problem with intersection detection 1 Answer
Array index out of range error 1 Answer
Few problems with arrays 1 Answer
Toggle Control between multiple turrets 0 Answers