• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Avash · Jan 04, 2015 at 02:20 PM · c#array

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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); 

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pako · Jan 04, 2015 at 03:19 PM 0
Share

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.

avatar image Avash · Jan 04, 2015 at 03:34 PM 0
Share

http://msdn.microsoft.com/en-us/library/vstudio/bb358985%28v=vs.100%29.aspx http://msdn.microsoft.com/en-us/library/vstudio/bb503062%28v=vs.100%29.aspx

o_O Why unity doesn't have these?

avatar image pako · Jan 04, 2015 at 03:58 PM 0
Share

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!

avatar image Avash · Jan 04, 2015 at 04:28 PM 0
Share

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.

avatar image pako · Jan 04, 2015 at 05:03 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A problem with intersection detection 1 Answer

Picking a Random Order for Levers 1 Answer

Array index out of range error 1 Answer

Few problems with arrays 1 Answer

Toggle Control between multiple turrets 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges