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 JNetRocks1316 · Dec 31, 2013 at 02:47 AM · arraypositionlisttimeteleport

How to store previous locations in a list to teleport?

Basically: I have a character I want to be able to use an ability and they will 'snap' back to a position 5 seconds ago.

I understand the concept of how to do this: check the players location every second, store it in a list with 5 indexes (one for each second). If the player presses the button, set their transform position to the oldest index.

What I'm having trouble with is figuring out the code to actually get the list functioning in a way that it saves only the latest three values rather than appending infinite numbers onto the end.

Any help would be appreciated!

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

2 Replies

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

Answer by HappyMoo · Jan 01, 2014 at 05:57 AM

 using System.Collections.Generic;
 
 private int maxLength = 20; // Oh what the heck, let's save 4 times per second ;)
 private Queue<Vector3> positions = new Queue<Vector3>(maxLength+1);
 private Queue<Quaternion> rotations= new Queue<Quaternion>(maxLength+1);
 
 void Store(Transform trans)
 {
   positions.Enqueue(trans.position);
   rotations.Enqueue(trans.rotation);
   if (positions.Count>maxLength)
   {
     positions.Dequeue();
     rotations.Dequeue();
   }
 }
 
 void Teleport(Transform trans)
 {
   trans.position = positions.Peek();
   trans.rotation = rotations.Peek();
 }
Comment
Add comment · Show 3 · 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 JNetRocks1316 · Jan 01, 2014 at 08:13 AM 0
Share

This is epic. Works really well on storing the information! I'm new to program$$anonymous$$g so I hadn't heard of Queue before!

Thanks for this. :)

avatar image HappyMoo · Jan 01, 2014 at 12:09 PM 0
Share

No, that's exactly what you want, because Queue adds new items at the end.... so the first is always the oldest. Cheers!

edit: Oh wait, you already edited this... not fully awake yet :D

avatar image JNetRocks1316 · Jan 02, 2014 at 02:59 AM 0
Share

Haha, yeah I thought it was giving me the newest one but it was just a problem with the way I had it set up. I made some changes and it's working beautifully.

I wish I could upvote this but not enough karma yet! I'll be sure to bookmark it and come back and vote when I can. :)

avatar image
0

Answer by trs9556 · Dec 31, 2013 at 03:05 AM

There is probably a more efficient way of doing it but because you already know it will be a max of 5 you can hard code a converter. So Make 2 arrays, a temp array and the actual array of locations.

When you add a new value to the "actual array" temporarily set all the current array values (except the one you are deleting) to the temp array. Delete the actual array then, and then make the temp array equal the actual array plus the current location.

Comment
Add comment · Show 1 · 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 JNetRocks1316 · Jan 01, 2014 at 04:48 AM 0
Share

Thanks that seems like a good approach. I was hoping there was a way to just say "insert" the new variable at index 0 and then 'shift' everything else down one place. ;P Guess it's not that easy! Thanks for the help. :)

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

20 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

Related Questions

A node in a childnode? 1 Answer

How to draw text based on text above it? 0 Answers

get GameObject at 3 position in list 1 Answer

Can you set a string length when declaring it to a certain other number? 2 Answers

Array in Arrays (Item info type) 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