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 RaulG · Jan 13, 2015 at 08:17 AM · movementvector3list

Vector3[]?

A friend of mine was showing me a few things that you could do. This was one of them:

 Vector3[] curPos = new Vector3[place];
         timeI += Time.deltaTime;
         float z = omegaZ * timeI;
         float y = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * timeI));
         eSpawn.eSnake [place].transform.position = new Vector3 (0.0f, y + curPos[place].y , z + curPos[place].z);
         place++;
 
         if (place == 5) 
         {
             place = 0;
         }


Problem is, it doesn't seem to work as I thought it did. And everytime the code reaches the line

 eSpawn.eSnake [place].transform.position = new Vector3 (0.0f, y + curPos[place].y , z + curPos[place].z);


It stops running.

What am I doing wrong?

I'm basically trying to get the current position of each object in a list, and then change that position. Otherwise it snaps to 0,0,0 which doesn't work well for what I want.

Comment
Add comment · Show 8
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 mattyman174 · Jan 13, 2015 at 08:28 AM 0
Share
 eSnake [place]

Remove the space in between these 2?

avatar image GameVortex · Jan 13, 2015 at 08:34 AM 0
Share

@mattyman174 Coding really doesn't care about whitespace. If it was to care it would cause a compiler error ins$$anonymous$$d and the code would not run at all.

avatar image nu-assets · Jan 13, 2015 at 08:38 AM 2
Share

The Vector3 array (curPos) has a fixed size of 'place'. That means the largest index you can read out of this array is [place - 1] since the first element in the array is zero (0).

So if you want to get last item rewrite the line 5 as follow:

 eSpawn.eSnake [place].transform.position = new Vector3 (0.0f, y + curPos[place - 1].y , z + curPos[place - 1].z);

And the other thing to check is: Does the eSnake array/list have the right size.

You could do the incrementation in modulus space:

 place = ++place % curPos.Length;

This way you ensure that the index does not overflow the size of the array.

avatar image Berenger · Jan 13, 2015 at 08:39 AM 1
Share

How does it stops running ? Do you get an error message in the console ? An exception ? NullRef or ArgumentOutOfRange maybe ? Or does it leaves the function and just keep going, ignoring your code ? By the way, is that piece of code inside a Unity loop (Update, for instance), or a ponctual function (Start) ?

avatar image Berenger · Jan 13, 2015 at 09:15 AM 1
Share

Then Wittwism is right

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RaulG · Jan 13, 2015 at 09:47 AM

 public int place;
     public List<Vector3> posList = new List<Vector3>();
 
     public void Snaker()
     {
         posList.Add (new Vector3 (eSpawn.eSnake [place].transform.position.x,
                                   eSpawn.eSnake [place].transform.position.y,
                                   eSpawn.eSnake [place].transform.position.z));
 
         Vector3 v = posList [0];
 
         timeI += Time.deltaTime;
         float z = omegaZ * timeI;
         float y = Mathf.Abs (amplitudeY * Mathf.Sin (omegaY * timeI));
         eSpawn.eSnake [place].transform.position = new Vector3 (0.0f, y + v.y , z + v.z);
         place++;
         
         if (place == 5) 
         {
             place = 0;
         }
         
     }

I switched over to a list, changed posList to 0, and parented the objects to a main object. Works semi right. Now I just need to add a delay between movements and I think it'll be okay.

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

29 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

Related Questions

A node in a childnode? 1 Answer

Plotting particle movement Time/keyframe issue 0 Answers

Stopping Vector3.Lerp 3 Answers

Choppy y-axis crane movement when grabbing food, not dropping food on target 0 Answers

GameObject wont stop rotating ? 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