- Home /
Variable values in Variable names
I am trying to fill an array with a large amount of Transforms from gameobjects in my scene. The purpose of this is to have a large amount of destinations I can LERP the main Camera to, in response to button press. What I am hoping would be possible is a more intelligent, and less manual, way of finding these Transforms, and adding them to the array. What I have now works, but is very tedious:
using UnityEngine;
using System.Collections;
public class CommandBar : MonoBehaviour {
public Transform pos1;
public Transform pos2;
private ArrayList posArrList = new ArrayList ();
private int curr = 0;
public Transform cam;
private Rect lftRect = new Rect(Screen.width*1390/1600, Screen.height*785/900, Screen.width/16, Screen.height/9);
private Rect rhtRect = new Rect(Screen.width*1490/1600, Screen.height*785/900, Screen.width/16, Screen.height/9);
void Start () {
posArrList.Add (pos1);
posArrList.Add (pos2);
}
void OnGUI()
{
if ((Event.current.type == EventType.MouseUp) && lftRect.Contains (Event.current.mousePosition)) {
curr--;
}
if ((Event.current.type == EventType.MouseUp) && rhtRect.Contains (Event.current.mousePosition)) {
curr++;
}
}
void Update () {
//
MoveCam((Transform)posArrList[curr]);
}
void MoveCam (Transform camDest) {
transform.position = Vector3.Lerp(transform.position, camDest.transform.position, Time.deltaTime * 3f);
}
}
Is it possible to at least add the many pos' (positions) to the array in a less "I'll just type everything" way, considering I will most likely be using 50+ possible destinations in the finished script?
Yes, I need the camera to go from specific position to specific position.
Answer by Catlard · May 29, 2014 at 10:24 AM
Here's what I would do:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class TransformPositionGetter : MonoBehaviour {
private List<Transform> _childTransforms;
public Transform _parentToFindChildrenFrom;
void Start () {
_childTransforms = new List<Transform>();
foreach(Transform child in _parentToFindChildrenFrom) {
_childTransforms.Add(child);
}
//you could use _childTransforms.ToArray() here, if you want.
//if you want to get a transform's position, just get it this way: childtransforms[3].position;
_childTransforms = _childTransforms.OrderBy(x => x.name).ToList<Transform>();
//this line uses linq to sort the list alphabetically, by name. If I were to translate this
//line to english, it would read;
//_childTransforms = itself, but ordering each x such that they are in order of x.name.
//so, if you wanted to change the transforms to be ordered by their y position, you would instead
//use this line of code:
//_childTransforms = _childTransforms.OrderBy(x => x.position.y).ToList<Transform>();
}
}
Everybody happy?
I'll just juggle with it a bit, see if it does everything I hope it does, but thanks for the speedy answer, it seems like a very promising solution =D
Indeed, it works like a charm. I'd like to point out a little detail, just for future reference: The order of the children in the list is deter$$anonymous$$ed by the order in which they were placed under the parent object (or so it seems to me), and not the order in which they lie under it, or alphabetical order.
Ah, well, in that case you'll probably want to sort them using linq. I'll update my original answer. Updated!
Answer by Xalemich · May 29, 2014 at 11:21 AM
I'll juggle with it a bit, but it does look to be a promising solution =) Thanks a bunch for the speedy reply!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to sync a variable Transform via photon? 0 Answers
Converting this JS code to C# 1 Answer
Multiple Cars not working 1 Answer