Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Kurokev · May 21, 2018 at 12:21 AM · rotationpositionyield waitforseconds

Return an object's position and rotation to what it was a certain amount of time ago

I have an object that is moved by following the player's mouse position. After a certain event I need the object to return to it's prior position/rotation from a certain amount of time ago. I know to do this I probably need to constantly keep track of the object's transform somehow. So far I have this:

 public Transform earlierTransform;
 public float waitTime = 2f;

 void Update () {
         StartCoroutine (SaveEarlierTransform ());
         if (Input.GetKeyDown (KeyCode.Space)) {
                 //set position and rotation equal to what it was earlier
                 transform.position = earlierTransform.position;
                 transform.rotation = earlierTransform.rotation;
         }
 }
 
 IEnumerator SaveEarlierTransform () {
         yield return new WaitForSeconds (waitTime);
         earlierTransform = transform;
 }
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
0

Answer by winterfluxstudio · May 21, 2018 at 01:10 AM

 // reference the object in question
 Public GameObject myObject;

 // reference object's X, Y, Z values
 public float initialPositionX;
 public float initialPositionY;
 public float initialPositionZ;
 
 void Start()
 {
     // set the initial position of myObject for X, Y and Z
     initialPositionX = myObject.tranform.position.x;
     initialPositionY = myObject.tranform.position.y;
     initialPositionZ = myObject.tranform.position.z;
 }

 // call this method when you want to return the object to its original position
 public void MoveObjectBack()
 {
     // move it back to its initial position
     myObject.transform.position = new Vector3(initialPositionX, initialPositionY, initialPositionZ);
 }



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

Answer by Deeblock · May 21, 2018 at 07:06 AM

Depending on how accurate you want the position or rotation to be, you can choose to store the object's rotation and transform every few seconds or less.

 public float delayBetweenRecords; // Delay between storing positions and rotation
 public float timeBeforePresent; // Amount of time to jump back
 public bool recordCheck; // If false, we stop recording positions
 
 private Vector3 pastPosition;
 private Vector3 pastRotation;
 private IEnumerator saveRecord; // Reference to our coroutine
 
 void Start() {
     Invoke("SaveRecordHandler", timeBeforePresent);
 }
 
 void Update() {
     if (Input.GetKeyDown (KeyCode.Space)) {
         transform.position = pastPosition;
         transform.rotation = pastRotation;
     }
 }
 
 private void SaveRecordHandler() {
     // If coroutine is already running, we stop it
     if (saveRecord != null) {
         StopCoroutine(saveRecord);
         saveRecord = null;
     }
     
     if (recordCheck) {
         saveRecord = SaveRecord();
         StartCoroutine(saveRecord);
     }
 {
 
 private IEnumerator SaveRecord() {
     while (recordCheck) {
         pastPosition = transform.position;
         pastRotation = transform.rotation;
         yield return new WaitForRealtimeSeconds(delayBetweenRecords);
     }

     // Reset variable
     saveRecord = null;
 }
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

127 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

Related Questions

How do I rotate an object around another to have its opposite ? 0 Answers

how to set rotation / position of an object on trigger? 2 Answers

if (transform.rotation == Vector3(0,0,0)) doesn't work! 2 Answers

Player not facing the mouse correctly 1 Answer

How to move camera up and down 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