- Home /
move a camera to different positions of a sorted array
hi, i am sorry but my brain is mush today. In the last days I was wasting my time try to write a "stone age script"... what you can see Here now i know its better to move the cam instead switch between multiple cams. but with my skills at this moment iam stuck total. Perhaps someone here help me to solve my brain knot. - I am grateful for every tip
given: i have a sorted list of pages with different positions. the pages name have numbers: page1, page2 ... page86 now I'm trying to move my maincamera in two different modes to the positions of the pages.
first: click on a button & jump from page1 to page2, click again from page 2 to page3 etc.
second: click on a button named page 47 -> and jump with maincam to 47.
this is a scipt I try to go on writing
private var pages : GameObject[];
public var speed = 1.5;
function Start() {
pages = GameObject.FindGameObjectsWithTag("Pages");
FollowRoute();
}
function FollowRoute() {
for (var go : GameObject in pages) {
var tr : Transform = go.transform;
while (transform.position != tr.position) {
transform.position = Vector3.MoveTowards(transform.position, tr.position, Time.deltaTime * speed);
yield;
}
}
}
Does this piece of code work? Where is the code for the input handling?
it works, the snippet moves only the cam along the array. here is a problem - because the array is not sorted: 0 = page19, 1 = page5, 2= page66 etc.... and i dont understand the sort stuff in other threads like "import linq" etc
at this moment there is no input handling :/
yes. and then move my cam to the array data with a eventtrigger button.
Answer by d2 · Mar 05, 2015 at 07:01 PM
I dont know JavaScript but in c#, you can try this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Example : MonoBehaviour {
public List<GameObject> SortedList;
void Awake(){
List<GameObject> temp = GameObject.FindGameObjectsWithTag("Page").ToList();
SortedList = temp.OrderBy(page => page.name.Length).ThenBy(page => page.name).ToList();
}
}
ow yes, this works perfect thank you so much!! - i have to learn more c#! maybe you have a tip for me how i use this snippet with your lines to move my cam through the array with a button in c#?
using UnityEngine;
using System.Collections;
public class AnimateCamera1 : $$anonymous$$onoBehaviour {
public float moveSpeed = 35.0f;
public GameObject targetObject;
void OnPointerDown ()
{
$$anonymous$$oveTowardsTarget(targetObject);
}
public void $$anonymous$$oveTowardsTarget(GameObject target)
{
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.transform.position, moveSpeed * Time.deltaTime);
}
}
ok, you could add 2 UI buttons, one for next page and one for last page, and make each one trigger the corresponding function of the next script:
public class Example : $$anonymous$$onoBehaviour {
public List<GameObject> SortedList;
public float moveSpeed = 35.0f;
private int currentIndex = 0;
void Awake(){
List<GameObject> temp = GameObject.FindGameObjectsWithTag("Page").ToList();
SortedList = temp.OrderBy(page => page.name.Length).ThenBy(page => page.name).ToList();
}
public void LastPage(){
if(currentIndex == 0){
Debug.Log ("There is no page before this one");
return;
}
currentIndex--;
$$anonymous$$oveCamera(SortedList[currentIndex].transform.position);
}
public void NextPage(){
if(currentIndex >= SortedList.Count -1){
Debug.Log ("This is the last Page");
return;
}
currentIndex++;
$$anonymous$$oveCamera(SortedList[currentIndex].transform.position);
}
void $$anonymous$$oveCamera(Vector3 targetPosition){
//Your movement Logic
//*I recommend you make this a coroutine
}
}
thank you for your help! I get no error but the cam is not moving...
the script is attached to the camera i tested the script with two UI Canvas buttons - 1. triggers the NextPage() 2. triggers the LastPage()
on button 2 the Debug.Log: "There is no page before this one" works
i add my lines to the: void $$anonymous$$oveCamera(Vector3 targetPosition) but nothing happens.
sry . yes iam sure that it works - probably its because I am sitting at the computer since this morning ;)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class CamChange : $$anonymous$$onoBehaviour
{
public List<GameObject> SortedList;
public float moveSpeed = 35.0f;
private int currentIndex = 0;
void Awake(){
List<GameObject> temp = GameObject.FindGameObjectsWithTag("Page").ToList();
SortedList = temp.OrderBy(page => page.name.Length).ThenBy(page => page.name).ToList();
}
public void LastPage(){
if(currentIndex == 0){
Debug.Log ("There is no page before this one");
return;
}
currentIndex--;
$$anonymous$$oveCamera(SortedList[currentIndex].transform.position);
}
public void NextPage(){
if(currentIndex >= SortedList.Count -1){
Debug.Log ("This is the last Page");
return;
}
currentIndex++;
$$anonymous$$oveCamera(SortedList[currentIndex].transform.position);
}
void $$anonymous$$oveCamera(Vector3 targetPosition)
{
//Your movement Logic
//*I recommend you make this a coroutine
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, transform.position, moveSpeed * Time.deltaTime);
}
}
Your answer
Follow this Question
Related Questions
Label an array of buttons with an array of strings 2 Answers
[Solved]Using an array of two buttons? 1 Answer
Change color of all buttons listed in array 3 Answers
Array out of range! 1 Answer
Creating Buttons from a Button created from an Array 1 Answer