Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 greenevo · Mar 05, 2015 at 05:24 PM · arraymovement scriptbuttons

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

Comment
Add comment · Show 4
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 hexagonius · Mar 05, 2015 at 05:31 PM 0
Share

Does this piece of code work? Where is the code for the input handling?

avatar image greenevo · Mar 05, 2015 at 05:44 PM 0
Share

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 :/

avatar image d2 · Mar 05, 2015 at 06:07 PM 0
Share

What is the problem, you want the Array to be sorted?

avatar image greenevo · Mar 05, 2015 at 06:25 PM 0
Share

yes. and then move my cam to the array data with a eventtrigger button.

2 Replies

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

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();
 
     }
 }
Comment
Add comment · Show 6 · 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 greenevo · Mar 05, 2015 at 07:36 PM 0
Share

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

avatar image d2 · Mar 05, 2015 at 08:03 PM 0
Share

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
     }
 
 }
avatar image greenevo · Mar 05, 2015 at 09:02 PM 0
Share

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.

avatar image d2 · Mar 05, 2015 at 09:21 PM 0
Share

this should work, could you post your script please..

avatar image greenevo · Mar 05, 2015 at 09:30 PM 0
Share

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);
     }
 }
Show more comments
avatar image
0

Answer by greenevo · Mar 05, 2015 at 10:25 PM

thank you so much d2 you were a great help!!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

22 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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges