Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by Lakish · Apr 29, 2016 at 06:56 PM · c#gameobjectpositionvector3array of gameobjects

how to save positions in Vector3 Array und move playerobjekt to the saved position in array's??

Hi,

im wiriting a Ludo in 3D in C# in Unity 5. I want to save all the positions on gamefield where the player can move in a vector3 array. I did it so:

 public class gameController : MonoBehaviour {
 
 public List<Vector3> playgroundForRed;

 void start () {
 List<Vector3> playgroundForRed= new List<Vector3>(); 
 
 // here i want to fill the arrayList with positions of gameobject I made for every position the player shall move (SFeld is the gameObject where the player can move)
 
 rotesSpielFeld.Add(GameObject.Find("SFeld1").transform.position);
 rotesSpielFeld.Add(GameObject.Find("SFeld1").transform.position);
 rotesSpielFeld.Add(GameObject.Find("SFeld2").transform.position);
 }

Then after the dice roll a 6 the player shall move out of his base to SFeld1 position: i did it like this in update():

 GameObject.Find("PlayerR1").transform.position = rotesSpielFeld[0];
 

But this don't work. The player doesn't move.

I realy don't know what I have to do now..


On the otherside I tried to save every single position direct in Unity like this:

![alt text][2]

and

The player jump to the Green Base ![alt text][1]

(This position isn't saved in the vector3 array) - there is no position where he moved ^^ But the Player has to go to the first red field on the Playground.


But when I write

 GameObject.Find("PlayerR1").transform.position = GameObject.Find("SFeld1").transform.position;

The Player move to the right position, but then I would have a problem on the nice dice roll. Thats why why I thought to save the positions in the array so i can "just" add the dice number to the next index.

I realy hope someone can help me..

Best Regards!!

Luke [1]: /storage/temp/69099-movementredplayer1.jpg [2]: /storage/temp/69100-fillledarraylistwithpositions1.jpg

movementredplayer1.jpg (66.0 kB)
fillledarraylistwithpositions1.jpg (84.7 kB)
Comment
Add comment · Show 11
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 Ali-hatem · Apr 29, 2016 at 07:38 PM 1
Share
 public List<Vector3> playgroundForRed;
 void Start(){
 playgroundForRed= new List<Vector3>();
 }

& you are adding elements to rotesSpielFeld not playgroundForRed

avatar image Lakish Ali-hatem · Apr 29, 2016 at 09:22 PM 0
Share

Oh Sry I forget to translate - rotesSpielFeld, this is the original name i use, but i have translated for the question in "playgroundForRed". In the original code the Name is always "rotesSpielFeld".

avatar image Lakish · Apr 29, 2016 at 08:00 PM 0
Share

No Sry, you misunderstood me. In the Code the name is always rotesSpielFeld - I just translated it here once and forgott the rest. So your answer was just a hint for wrong Translation here -.-

avatar image Ali-hatem Lakish · Apr 29, 2016 at 08:08 PM 1
Share

again : the last solution is fine & simple what problem you have with the dice ? & stop answering use comments click replay under my comments.

avatar image Lakish Ali-hatem · Apr 29, 2016 at 09:30 PM 0
Share

awesome :) Thank You!! The First problem is fixed. So next one with the dice is:

The Player is now on his startposition. with: GameObject.Find("Würfel").GetComponent().aktuellerWert

I can read the number from the dice, when he is rolled (1-6). How can i move now the player from his startPosition to the next arrayposition. I think the best way is with a loop, but i don't know how to create the loop.

u know what i mean?

Show more comments
Show more comments
avatar image Ali-hatem Lakish · Apr 30, 2016 at 12:47 PM 1
Share

i am doing the same & i am @ the last steep stay tuned

avatar image Lakish Ali-hatem · Apr 30, 2016 at 02:01 PM 0
Share

I'm excited :D

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Ali-hatem · Apr 30, 2016 at 02:39 PM

so i made it with keyboard input you can edited as you want & the good news is all we need is one short script for all & don't worry values are different in each object since we assign values in inspector for each object :

 int currint;
 public List<Vector3> poslist = new List <Vector3>();// make 7 elements in inspector!
 public int dice;
 void Update(){
     if(my turn){ //assuming you made something to detect turn
         currint = poslist.FindIndex (d=>d == transform.position);
             if(Input.GetKeyDown(KeyCode.Space)){
               dice = Random.Range (1,6);
               transform.position = poslist[currint + dice];
             }
     }
 }

pay attention :

  1. because arrays & lists start from 0 i made 7 elements & the zero will hold the main position of player so adjust targeted positions from 1 to 6 as desired .

  2. you will face error if transform.position > poslist indexes i mean if you @ poslist[4] & the dice number is 4 player should go to poslist[8] which we don't have .

  3. so i don't know what is the limitation of positions & what should happen if player reaches the end .

Comment
Add comment · Show 9 · 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 Lakish · Apr 30, 2016 at 02:55 PM 1
Share

That's nice, I will try it in a few $$anonymous$$utes.

to be honest, I try to write a Ludo 3D game, so the playfield is 40 elements. I added them all in the vector3 arraylist. Like you said before.

 rotesSpielFeld = new List<Vector3>();
             rotesSpielFeld.Add(GameObject.Find("SFeld1").transform.position);
             .....
             rotesSpielFeld.Add(GameObject.Find("SFeld40").transform.position);

I will try it and wirte you back as soon as possible!

avatar image Ali-hatem Lakish · Apr 30, 2016 at 03:19 PM 0
Share

sorry it's my bad i didn't read the name of game "Ludo" but it's the same concept & you have to make end position to prevent more movements or destroy gameObject & add vectors in inspector it's visual & simple

avatar image Ali-hatem Ali-hatem · May 01, 2016 at 08:24 PM 1
Share

if player position i.e it's vector didn't match any array elements it will be treated as -1 element so have you missed something & after it go to position 6 & the dice roll again what happen & i might replay tomorrow .

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.

Follow this Question

Answers Answers and Comments

158 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 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 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 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 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 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 avatar image avatar image

Related Questions

Get the transform.position of a gameobject in an array? 2 Answers

Dot Product not equals 1 0 Answers

How To Make An Object Appear In Front Of Player Without Cloning? 1 Answer

gameobject transform position doesn't return the same vector3 2 Answers

Unique list of Vector3's 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